10

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
 )
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
antpaw
  • 15,444
  • 11
  • 59
  • 88

5 Answers5

22

With PHP 5.3.0+:

array_reduce($array, 'array_merge', array());
Clement Herreman
  • 10,274
  • 4
  • 35
  • 57
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    Beautiful one-liner, but I still have to go with Gordon's approach, it seems simpler to me. – Pekka Jan 27 '10 at 12:25
  • 4
    I like this one better than mine, because it works for an arbitrary amount of arrays on the first level – Gordon Jan 27 '10 at 12:25
  • wow this looks cool, but it doesnt work for me :( array_merge() [function.array-merge]: Argument #1 is not an array, but im 100% sure its the same array form the example. – antpaw Jan 27 '10 at 12:28
  • Works perfectly here, but I have a different structure: Before: `array(2) { [0]=> array(1) { ["a"]=> int(1) } [1]=> array(1) { ["b"]=> int(2) }}` After: `array(2) { ["a"]=> int(1) ["b"]=> int(2)}` – Ignacio Vazquez-Abrams Jan 27 '10 at 12:33
  • I also wonder why this works, as I tried it myself in another context without success. In comments on the `array_reduce` documentation site, someone says: "If $initial parameter is not an int, array_reduce passes 0 (zero) to the callback function". This way exactly my problem. So why does it work in your case? Also the method signature says: `(array $input , callback $function [, int $initial ])` note **`int $initial`**. – Felix Kling Jan 27 '10 at 13:34
  • I have PHP 5.3.1 here. It might be broken in some older versions. – Ignacio Vazquez-Abrams Jan 27 '10 at 13:37
  • Ok I think I found it, this was introduced in PHP 5.3.0 : "Changed array_reduce() to allow mixed $initial." Maybe you should put this in your answer. – Felix Kling Jan 27 '10 at 13:39
5

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;
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
4

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];
Gordon
  • 312,688
  • 75
  • 539
  • 559
1

This is also a beautifull one liner

$array = new RecursiveArrayIterator($yourArray);
streetparade
  • 32,000
  • 37
  • 101
  • 123
  • this also doesn't work for me, it doesn't change the input array in any way. – antpaw Jan 27 '10 at 12:34
  • You'd have to wrap that into an RecursiveIteratorIterator as well to be able to iterate over the first level elements. Also, $array won't be an array but, like the classname suggests, an iterator, wrapping an array. – Gordon Jan 27 '10 at 12:38
1

With PHP 5.6.0+:

$new = array_merge(...$array);

(A more general version of Gordons approach.)

Johannes Buchholz
  • 1,857
  • 19
  • 34