Add array_map functions for mapping associative arrays whilst maintaining their original array keys
composer require pfrembot/array-map-assocrequire_once 'vendor/autoload.php';
use Pfrembot;
$array = [
'foo' => 1,
'bar' => 2,
'baz' => 3,
];
$newArray = Pfrembot\array_map_assoc($array, function($value, $key) {
return $value * 2;
});
var_dump($newArray);
/*
Outputs:
array(3) {
'foo' =>
int(2)
'bar' =>
int(4)
'baz' =>
int(6)
}
*/Applies the user defined callback to each key/value pair in the array, and maps the callback result as the new array value
array array_map_assoc ( array $array , callable $callback )array
The input array
callback
The callback to be applied to each key/value pair in $array
mixed callback( mixed $value , mixed $key )-
value : Holds the value of the current iteration.
-
key : Holds the key of the current iteration.
-
Note: Many internal functions (for example strtolower()) will throw a warning if more than the expected number of argument are passed in and are not usable directly as a callback.
Returns the values of the original array mapped back to the original array keys