tags:

views:

81

answers:

4

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
 )
+2  A: 

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
+1 That's clever!
Pekka
+3  A: 

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
@Pekka - yep, I'm just not with it today.
karim79
@karim79, that happens sometimes. But @Gordon beat us both, his solution is definitely the best.
Pekka
+8  A: 

With PHP 5.3.0+:

array_reduce($array, array_merge, Array());
Ignacio Vazquez-Abrams
Beautiful one-liner, but I still have to go with Gordon's approach, it seems simpler to me.
Pekka
I like this one better than mine, because it works for an arbitrary amount of arrays on the first level
Gordon
Good point. +1.
Pekka
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
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
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
I have PHP 5.3.1 here. It might be broken in some older versions.
Ignacio Vazquez-Abrams
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
+1  A: 

This is also a beautifull one liner

$array = new RecursiveArrayIterator($yourArray);
streetparade
this also doesn't work for me, it doesn't change the input array in any way.
antpaw
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