0

I have a problem. Maybe it will be simple to solve.

There is an array looking like this:

(int) 0 => array(
    (int) 0 => array(
        'Post' => array(
            'name' => 'value'
        )
    )
),
(int) 1 => array(
    (int) 0 => array(
        'Post' => array(
            'name' => 'value'
        )
    ),
    (int) 1 => array(
        'Post' => array(
            'name' => 'value'
        )
    )
)

That needs to look like this:

(int) 0 => array(
        'Post' => array(
            'name' => 'value'
        )
    )
(int) 1 => array(
        'Post' => array(
            'name' => 'value'
        )
    )
(int) 2 => array(
        'Post' => array(
            'name' => 'value'
        )
    )

I tried array_shift() and directly after that, array_values(), but that gave me only the first post.

I assume the order was "0, 0, 1", so PHP cut it after the first one.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Karl
  • 410
  • 11
  • 25
  • You should clarify if your array is expected to allways maintain this structure, or if the number of elements of the array or the depth of each array is expected to change. This is the difference between a very simple yet rigid solution to a slightly more complex but flexible solution. – cernunnos Mar 18 '13 at 11:08
  • Yes, it needs to be very flexible. Decezes answer below did the trick very elegantly. Thanks anyway – Karl Mar 18 '13 at 11:15
  • Apparently you need flexibility on the number of arrays (or original array size), but not on the depth of the arrays. I like the same answer for that. – cernunnos Mar 18 '13 at 11:24
  • Demo of @Deceze's answer and another technique from the dupe: https://3v4l.org/IW948 (This page adds no new value to SO and can be safely scrubbed.) – mickmackusa Oct 05 '20 at 10:13

2 Answers2

3
$array = call_user_func_array('array_merge', $array);
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Why use call_user_func_array() instead of just array_merge()? – cernunnos Mar 18 '13 at 11:06
  • What this essentially does is `$array = array_merge($array[0], $array[1], ...)` for an arbitrary number of elements in `$array`. – deceze Mar 18 '13 at 11:08
  • Wow, that is genius! Is that flexible for 0 to n numbers of posts in that array while arbitrary depths are given? Well, it seems that way :) – Karl Mar 18 '13 at 11:15
  • @Karl It basically removes the *first level* of arrays from a data structure like yours. It doesn't merge arbitrarily *deep* arrays (well, the arrays can be arbitrarily deep, it doesn't affect the outcome). `$array` should contain at least one element, otherwise `array_merge` will complain. – deceze Mar 18 '13 at 11:18
  • Nice solution, takes advantage of the fact that array_merge accepts a variable list array parameters. – cernunnos Mar 18 '13 at 11:22
  • Really great, it´s exactly what I need. +1000 for that answer. – Karl Mar 18 '13 at 11:27
0

This is what I have tried ..

      <?php
              $test = array(
                              array(array('post'=>array('name','value'))),
                              array(array('post'=>array('name','value')),array('post'=>array('name','value')),),
                           );
              print_r($test);


              $test_arr = array();

              foreach($test as $value)
              {
                 foreach($value as $subvalue)
                 {
                    $test_arr[] = $subvalue; 
                 }
              }

              print_r($test_arr);
              ?> 
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
  • Please post code *here*; this answer needs to be useful by itself, not depending on information elsewhere. – deceze Mar 18 '13 at 11:13