php - Why does array_map() with null as callback create an "array of arrays"? -
today learned special case of array_map()
in php, mentioned side note in documentation:
example #4 creating array of arrays
<?php $a = array(1, 2, 3, 4, 5); $b = array("one", "two", "three", "four", "five"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?>
the above example output:
array ( [0] => array ( [0] => 1 [1] => 1 [2] => uno ) [1] => array ( [0] => 2 [1] => 2 [2] => dos ) [2] => array ( [0] => 3 [1] => 3 [2] => tres ) [3] => array ( [0] => 4 [1] => 4 [2] => cuatro ) [4] => array ( [0] => 5 [1] => 5 [2] => cinco ) )
if array argument contains string keys returned array contain string keys if , if 1 array passed. if more 1 argument passed returned array has integer keys.
but that's it. no more explanation. understand, same as
$d = array_map(function() { return func_get_args(); }, $a, $b, $c);
but why want or expect default behavior? there technical reason why works that, side effect implemtation? or random "let's make function 1 more thing" decision (looking @ you, array_multisort()
)?
this appears special case in _array_map_, it's documented in example. null not allowed callback (if try use call_user_func()
reports error), it's allowed in _array_map()_. treats null
meaning should create array of arguments.
this useful because array
not valid callback, because it's language construct, not function. can't write:
$d = array_map('array', $a, $b, $c);
Comments
Post a Comment