1

It's probably beginner question but I'm going through documentation for longer time already and I can't find any solution and i have an array which is multidimensional given below format.

/* This is how my array is currently */
Array
(
  [0] => Array
     (
        [0] => Array
            (
                [title] => a
                [title_num] =>1 
                [status] => 1
            )
        [1] => Array
            (
                [title] => Mr
                [title_num] => 82
                [status] => 1
            )
     )
    [1] => Array
    (
        [0] => Array
            (
                [title] => b
                [title_num] =>25 
                [status] => 2
            )
        [1] => Array
            (
                [title] => c
                [title_num] =>45
                [status] => 2
            )
     )
)

I want to convert this array into this form

 /*Now, I want to simply it down to this*/
    Array
    (
        [0] => Array
            (
                [title] => a
                [title_num] =>1 
                [status] => 1
            )
        [1] => Array
            (
                [title] => Mr
                [title_num] => 82
                [status] => 1
            )
        [2] => Array
            (
                [title] => b
                [title_num] =>25 
                [status] => 2
            )
        [3] => Array
            (
                [title] => c
                [title_num] =>45
                [status] => 2
            )
     )

I have tried array_flatten,array_map PHP built in function A link or anything to point me in the right direction will be highly appreciated

Naveen N
  • 29
  • 2
  • 7

3 Answers3

1

Like this:

$i=0;
foreach ($array as $n1) {
        foreach ($n1 as $n2) {
            $newArr[$i]['title']=$n2['title'];
            $newArr[$i]['title_num']=$n2['title_num'];
            $newArr[$i]['status']=$n2['status'];
        }
        $i++;
    }

Or simpler as suggested in the comments

for ($i=0; $i < count($array); $i++) { 
    foreach ($array[$i] as $n2) {
        $newArr[$i]['title']=$n2['title'];
        $newArr[$i]['title_num']=$n2['title_num'];
        $newArr[$i]['status']=$n2['status'];
    }
}
Zeke
  • 1,281
  • 1
  • 18
  • 26
  • 1
    Shouldn't the second `foreach` use `$n1` instead of `$array`? Also, why not `for`? Using `for` the counter is explicitly created in the parameters. – Zeke Jan 25 '18 at 15:08
  • Corrected, you were right, thanks <3 – José Lopez Coronado Jan 25 '18 at 15:09
  • De nada, you're welcome. I actually prefer your answer as opposed to the others, but I still think `for` is better haha. – Zeke Jan 25 '18 at 15:11
  • In this case I think the same thing, but I'd decided to go for my classic haha, is beautiful how we can do the same thing in so many ways. – José Lopez Coronado Jan 25 '18 at 15:14
  • @Zeke added the way you suggested – José Lopez Coronado Jan 25 '18 at 15:21
  • 1
    Well, I would have used 2 `for` loops, and I would have saved `count($array)` and `count($n1)` in a variable to avoid too many function calls... but I guess that works too. I mean, I think about legibility, efficiency, practicality, etc.. so those small details make a huge impact in greater proportions. – Zeke Jan 25 '18 at 15:22
  • haha it will work, but yes, I did it backwards – José Lopez Coronado Jan 25 '18 at 15:27
  • Check the edits, I'm not exactly happy about being mentioned in the answer. Especially when I wouldn't have written that block of code. – Zeke Jan 25 '18 at 15:30
1

Here is another trick to solve your problem,

function custom_filter($array) { 
    $temp = [];
  array_walk($array, function($item,$key) use (&$temp){
      foreach($item as $value)
         $temp[] = $value;
  });
  return $temp;
} 

array_walkApply a user supplied function to every member of an array

Here is working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
1

here you go

$result = [];
foreach($array as $arr)
{
    $result = array_merge($result , $arr);
}

var_dump($result);
Ali Faris
  • 17,754
  • 10
  • 45
  • 70