Hi, all
$myarr=["22"=>1,"33"=>2]
When I use
$return=array_map(function($v,$k){ //TODO},$myarr,array_keys($myarr));
How do $return keep the original key?
I found that for
array_map(function($v){ //TODO},$myarr);
It is possible to keep the key.
Reply content:
Hi, all
$myarr=["22"=>1,"33"=>2]
When I use
$return=array_map(function($v,$k){ //TODO},$myarr,array_keys($myarr));
How do $return keep the original key?
I found that for
array_map(function($v){ //TODO},$myarr);
It is possible to keep the key.
Array_map () This function when there is only one parameter in the callback function (that is, when there is only one array), the callback function operates independently of the value of the array, or it can return a key, but when Array_map () operates two arrays, you also return the key? It is impossible to think about it, the function is the key to return the first array? Or the key of the second array? So multiple arrays will not give you the key, just give you return value ...
1 want to ask is, what kind of function do you want to realize?
2 If you want to save the key value of the first array, you can do this:
$arr1 = array('b' => 'banana', 'g' => 'grage');$arr2 = array('a' => 'apple', 'p' => 'pear');$return = array_map(function($a, $b){ // to do something return 'I love ' . $a . ' and ' . $b;}, $arr1, $arr2);$return = array_combine(array_keys($arr1), $return);