Question

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] => Array
     (
     )

 [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
 )
Was it helpful?

Solution

With PHP 5.3.0+:

array_reduce($array, 'array_merge', array());

OTHER TIPS

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top