今天我学习了PHP中array_map()
的一个特殊用法,这在文档的旁注中提到:
Example #4 Creating an 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 will output:
Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) )
If the array argument contains string keys then the returned array will contain string keys if and only if exactly one array is passed. If more than one argument is passed then the returned array always has integer keys.
但就是这样。没有更多的解释了。我理解,这与以下代码的作用相同:
$d = array_map(function() { return func_get_args(); }, $a, $b, $c);
但是为什么有人希望或者期望这是默认行为呢?是否有技术原因导致它工作方式如此,例如从实现的副作用中?还是只是随意决定“让这个函数做更多的事情”(看看你,array_multisort()
)?