Use Associative Array Keys in array_map: 3 PHP Code Examples

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Using Associative Array Keys in PHP array_map

We have already seen the PHP array_map and some important related caveats.

In a nutshell, the array_map function applies a callback to every element of the given arrays.

One essential quirk of the function is that it passes the values of the given arrays to the callback function. There is no inherent way to pass a key as a parameter to the callback function.

PHP array_map
PHP array_map

This article will show how to use associative array keys in PHP array_map function.

To get the most out of this article, we recommend reading our article about array_map PHP.

Multiple arrays usage – A review

Let’s review the array_map function when it gets more than one array as an argument. This review is crucial because we will use two arrays to use associative keys in the array_map function.

The example gets two arrays; one consists of Roman numerals, and the other has equivalent numbers in English. It uses array_map to get the corresponding values from both arrays simultaneously.

Associative Array Keys array_map() PHP Code Example

<?php
$roman_numerals = ["I", "II", "III", "IV", "V"];
$english_numbers = ["One", "Two", "Three", "Four", "Five"];
 
print_r(array_map(fn($rom, $eng) => "The Roman Numeral {$rom} is {$eng} in English", $roman_numerals, $english_numbers));
 
/*
OUTPUT
Array
(
    [0] => The Roman Numeral I is One in English
    [1] => The Roman Numeral II is Two in English
    [2] => The Roman Numeral III is Three in English
    [3] => The Roman Numeral IV is Four in English
    [4] => The Roman Numeral V is Five in English
)
*/
?>

Voila!

Both these arrays were equal, and the array_map PHP function mapped them using the values from both the arrays.

But, what if the two arrays were unequal?

Luckily, the function is robust enough to handle them. It pads the shorter array with null values. The following example demonstrates it.

<?php
$roman_numerals = ["I", "II", "III", "IV", "V", "VI"];
$english_numbers = ["One", "Two", "Three", "Four", "Five"];
 
print_r(array_map(fn($rom, $eng) => "The Roman Numeral {$rom} is {$eng} in English", $roman_numerals, $english_numbers));
 
/*
OUTPUT
Array
(
    [0] => The Roman Numeral I is One in English
    [1] => The Roman Numeral II is Two in English
    [2] => The Roman Numeral III is Three in English
    [3] => The Roman Numeral IV is Four in English
    [4] => The Roman Numeral V is Five in English
    [5] => The Roman Numeral VI is  in English
)
*/
?>

The array_map assigns a null value to the missing sixth element of the second array, $english_numbers.

PHP arrays associative keys in PHP array_map function

So, after a quick review of passing multiple arrays arguments to the array_map function, we will extend this idea to pass array keys to the callback function.

Since the callback function doesn’t fetch the key, we have to find a workaround.

The idea is to pass the keys as a separate array to the PHP array_map. PHP has a function for separating keys into an array of its own, the array_keys function.

So, let’s move on to the example and try out this idea.

<?php
$developers = [
    "Anna" => "Javascript",
    "Edward" => "Java",
    "Rey" => "Python",
    "Sanjay" => "PHP",
    "Ali" => "C++",
    "Sarah" => "C#",
    "Bob" => "Android"
];
 
$dev_names = array_keys($developers);
 
$resultant = array_map(function($name, $tech) {
    return "{$name} is a {$tech} developer";
}, $dev_names, $developers);
 
print_r($resultant);
 
/*
OUTPUT
Array
(
    [0] => Anna is a Javascript developer
    [1] => Edward is a Java developer
    [2] => Rey is a Python developer
    [3] => Sanjay is a PHP developer
    [4] => Ali is a C++ developer
    [5] => Sarah is a C# developer
    [6] => Bob is a Android developer
)
*/
?>

Voila!

The idea works like a charm.

We had a $developers array with their names as keys and skills as values. So, the array_keys isolate the names into a separate array, $dev_names.

The array_map function gets $dev_names, and $developers arrays as arguments, just as we saw in the review section. The callback can fetch the values from both these arrays.

This idea is a neat workaround to overcome the inherent limitation of the array_map function.

Using Associative Array Keys in PHP array_map

In this article, we have seen how to use associative array keys in PHP array_map function. The article reviews the use of multiple arrays as arguments in the array_map, followed by the main tutorial of using arrays associative keys in array_map PHP.

The example uses the array_keys function to get the array of keys.

Finally, the keys array and the actual array are passed as arguments to the function. Hopefully, this article has been helpful. Stay tuned for more interesting articles and tutorials related to PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

How to merge 2 arrays with the same keys in PHP

PHP array_map: How to use in PHP with examples

Difference between array_combine and array_merge in PHP

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.