Short Version
There is:
$resultado = array_map(null, $arr["A"], $arr["B"], $arr["C"]);
or:
array_unshift($arr, null);
$resultado = call_user_func_array("array_map", $arr);
Long Version
There is indeed a native function that can do this and calls itself array_map
considering the particular case of passing null
as the first parameter, which corresponds to the callback. One of the examples of documentation (Example # 4) has an interesting note on this:
An interesting way to use this function is to construct an array of arrays, which can easily be done using NULL as the name of the callback function.
In this example, array_map
is called with null
and with 3 arrays
relevant:
$a = array(1, 2, 3, 4, 5);
$b = array("um", "dois", "tres", "quatro", "cinco");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
In that the result will construct an array of arrays with a value of each one intercaladamente:
Array
(
[0] => Array
(
[0] => 1
[1] => um
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => dois
[2] => dos
)
...
Applying this idea to your code could start with something simple indicating each of the indices to interpret, which correspond to the sub-arrays:
$arr = array(
'A' => array(10,20,30),
'B' => array(100,200,300),
'C' => array(1000,4000,9000)
);
$res = array_map(null, $arr["A"], $arr["B"], $arr["C"]);
print_r($res);
That gives you the following output:
Array
(
[0] => Array
(
[0] => 10
[1] => 100
[2] => 1000
)
[1] => Array
(
[0] => 20
[1] => 200
[2] => 4000
)
[2] => Array
(
[0] => 30
[1] => 300
[2] => 9000
)
)
View the result on Ideone
However it turns out to be rigid because strength does not only know the keys as the amount of elements it has. To work around this problem you can follow a slightly lower solution in the documentation that puts null
at the beginning of the array with array_unshift
"and then expand all parameters for the map function with call_user_func_array
:
$arr = array(
'A' => array(10,20,30),
'B' => array(100,200,300),
'C' => array(1000,4000,9000)
);
array_unshift($arr, null);
$res = call_user_func_array("array_map", $arr);
print_r($res);
That gives you exactly the same output.
See this solution also in Ideone
Detailing array_unshift
and call_user_func_array
The array_unshift
is only used to add a value at startup. So I figured I'd have an array like this:
$arr = Array(1, 2, 3, 4, 5);
When doing array_unshift($arr, null);
it will get null
at the beginning, as if it were built like this:
$arr = Array(null, 1, 2, 3, 4, 5);
The call_user_func_array
will pick up the indicated function and call it by passing all the values of the indicated array as parameters. Imagine you have the array defined above:
$arr = Array(null, 1, 2, 3, 4, 5);
By doing call_user_func_array("array_map", $arr);
it is as if you have directly called array_map
as follows:
array_map(null, 1, 2 , 3, 4, 5);
This would not be possible to do manually since the amount of parameters would change depending on the amount of elements in the array, which is actually the first example I gave with array_map(null, $arr["A"], $arr["B"], $arr["C"]);
.