Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's array_map including keys

Is there a way of doing something like this:

$test_array = array("first_key" => "first_value",                      "second_key" => "second_value");  var_dump(array_map(function($a, $b) { return "$a loves $b"; },           array_keys($test_array),           array_values($test_array))); 

But instead of calling array_keys and array_values, directly passing the $test_array variable?

The desired output is:

array(2) {   [0]=>   string(27) "first_key loves first_value"   [1]=>   string(29) "second_key loves second_value" } 
like image 840
José Tomás Tocino Avatar asked Oct 23 '12 17:10

José Tomás Tocino


People also ask

Does array map preserve keys?

The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.

How to modify data in array in php?

Definition and Usage. The array_replace() function replaces the values of the first array with the values from following arrays. Tip: You can assign one array to the function, or as many as you like. If a key from array1 exists in array2, values from array1 will be replaced by the values from array2.

What exactly is the the difference between array_map Array_walk and Array_filter?

The resulting array of array_map has the same length as that of the largest input array; array_walk does not return an array but at the same time it cannot alter the number of elements of original array; array_filter picks only a subset of the elements of the array according to a filtering function.


2 Answers

Not with array_map, as it doesn't handle keys.

array_walk does:

$test_array = array("first_key" => "first_value",                     "second_key" => "second_value"); array_walk($test_array, function(&$a, $b) { $a = "$b loves $a"; }); var_dump($test_array);  // array(2) { //   ["first_key"]=> //   string(27) "first_key loves first_value" //   ["second_key"]=> //   string(29) "second_key loves second_value" // } 

It does change the array given as parameter however, so it's not exactly functional programming (as you have the question tagged like that). Also, as pointed out in the comment, this will only change the values of the array, so the keys won't be what you specified in the question.

You could write a function that fixes the points above yourself if you wanted to, like this:

function mymapper($arrayparam, $valuecallback) {   $resultarr = array();   foreach ($arrayparam as $key => $value) {     $resultarr[] = $valuecallback($key, $value);   }   return $resultarr; }  $test_array = array("first_key" => "first_value",                     "second_key" => "second_value"); $new_array = mymapper($test_array, function($a, $b) { return "$a loves $b"; }); var_dump($new_array);  // array(2) { //   [0]=> //   string(27) "first_key loves first_value" //   [1]=> //   string(29) "second_key loves second_value" // } 
like image 160
eis Avatar answered Sep 22 '22 17:09

eis


This is probably the shortest and easiest to reason about:

$states = array('az' => 'Arizona', 'al' => 'Alabama');  array_map(function ($short, $long) {     return array(         'short' => $short,         'long'  => $long     ); }, array_keys($states), $states);  // produces: array(      array('short' => 'az', 'long' => 'Arizona'),       array('short' => 'al', 'long' => 'Alabama') ) 
like image 23
Kevin Beal Avatar answered Sep 22 '22 17:09

Kevin Beal