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

3
  • 2
    @Gareth Jones ,i checked this not working Commented Jan 25, 2018 at 15:05
  • 1
    @GarethJones It is not working as per OP way. Commented Jan 25, 2018 at 15:09
  • @NaveenN Thank you for checking Commented Jan 25, 2018 at 15:10

3 Answers 3

2

here you go

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

var_dump($result);
Sign up to request clarification or add additional context in comments.

2 Comments

this is straightforward!
@Akintunde007 Thank you , its working.. is there any built in function to do same without foreach
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.

Comments

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'];
    }
}

8 Comments

Shouldn't the second foreach use $n1 instead of $array? Also, why not for? Using for the counter is explicitly created in the parameters.
Corrected, you were right, thanks <3
De nada, you're welcome. I actually prefer your answer as opposed to the others, but I still think for is better haha.
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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.