Short Version
There is yes:
$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 actually a native function that can do this and calls if array_map
considering the particular case of passing null
as first parameter, which corresponds to callback. One of the examples of documentation (Example #4) has an interesting note to it:
An interesting way to use this function is in building an array of arrays, which can be easily done using NULL as the callback function name.
In this example the array_map
is called with null
and with the 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);
Where the result will construct an array of arrays with a value of each interspersed:
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 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 exit:
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
)
)
See the result in Ideone
However it turns out to be rigid because it forces not only know the keys as the amount of elements it has. To get around this problem you can follow a solution a little further down in the documentation that puts null
at the beginning of the array with array_unshift
and then expand all parameters to 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 exit.
See this solution also in Ideone
Detailing array_unshift
and call_user_func_array
The array_unshift
is only used to add a value to the start. So imagine you have an array like this:
$arr = Array(1, 2, 3, 4, 5);
In doing array_unshift($arr, null);
he’s gonna get null
at first, as if it had been built like this:
$arr = Array(null, 1, 2, 3, 4, 5);
The call_user_func_array
will take the given function and call it passing as parameters all values of the given array. Imagine you have the array defined above:
$arr = Array(null, 1, 2, 3, 4, 5);
In doing call_user_func_array("array_map", $arr);
It’s like he called directly the array_map
as follows:
array_map(null, 1, 2 , 3, 4, 5);
This would not be possible to do manually as 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"]);
.
Very good ! I tried to do by
foreach
but did not know this functionis_arrray
, there was no way. Although it works perfectly, the form of Isac, in my view, is simpler, and uses native functions. Even so, thank you so much ! + 1– rbz