how to unwrap an array in php
i have this array here:
Array
(
[0] => Array
(
[presentation] => Präsentationen
)
[1] => Array
(
[news] => Aktuelle Meldungen
[devplan] => Förderprogramme
[salesdoc] => Vertriebsunterlagen
)
[2] => Array
(
[user/settings] => Mein Account
)
[3] => Arr开发者_运维技巧ay
(
)
[4] => Array
(
[orders] => Projekte
)
)
i want to unwrap the first depth of the array to get this:
Array
(
[presentation] => Präsentationen
[news] => Aktuelle Meldungen
[devplan] => Förderprogramme
[salesdoc] => Vertriebsunterlagen
[user/settings] => Mein Account
[orders] => Projekte
)
With PHP 5.3.0+:
array_reduce($array, 'array_merge', array());
I guess the simplest way is to use a foreach
loop:
$resultArray = array();
foreach ($myArray as $array)
foreach ($array as $key => $element)
$resultArray[$key] = $element;
Try
array_merge($array[0], $array[1], $array[2], $array[3], $array[4]);
or
$new = $array[0] + $array[1] + $array[2] + $array[3] + $array[4];
This is also a beautifull one liner
$array = new RecursiveArrayIterator($yourArray);
With PHP 5.6.0+:
$new = array_merge(...$array);
(A more general version of Gordons approach.)
精彩评论