1

I have this nested array

$lists = [
  [
   {"id": 1, "name": one},
   {"id": 2, "name": two},
   {"id": 3, "name": three},
  ],
  [
   {"id": 4, "name": four},
   {"id": 5, "name": five},
   {"id": 6, "name": six},
  ]
]

What should i do to make this array into one like this.

[
   {"id": 1, "name": one},
   {"id": 2, "name": two},
   {"id": 3, "name": three},
   {"id": 4, "name": four},
   {"id": 5, "name": five},
   {"id": 6, "name": six},
]

I tried array_merge using this code

$numbers =[];
foreach ($lists as $list) {
    $numbers = array_merge($numbers, $list);
}

But it didn't work. It says that argument #2 is not an array.

8
  • Where's the index numbers you're referring to? I can't see it in the arrays you've posted? You should also clarify what "didn't work" means. Wrong result? Errors? Commented Dec 13, 2018 at 7:18
  • the numbers index consist the array in the first code. Sorry forgot to add Commented Dec 13, 2018 at 7:28
  • json_encode(array_merge(...json_decode($json, true)))- sandbox.onlinephpfunctions.com/code/… Commented Dec 13, 2018 at 7:40
  • question edited, please see and mybe it can help you identify my problem Commented Dec 13, 2018 at 7:48
  • $lists is invalid. Commented Dec 13, 2018 at 7:49

2 Answers 2

3

You can try this way. I hope it will be helpful

$lists =array(
 array(
    array("id"=> 1, "name"=> "one"),
    array("id"=> 2, "name"=> "two"),
    array("id"=> 3, "name"=> "three")
  ),
  array(
    array("id"=> 4, "name"=> "four"),
    array("id"=> 5, "name"=> "five"),
    array("id"=> 6, "name"=> "six")

  )
);

    $numbers =array();
foreach ($lists as $list) {

   foreach ($list as $c) {
    array_push($numbers,$c);
   }
}

echo "<pre>";
print_r($numbers);
Sign up to request clarification or add additional context in comments.

Comments

1

There are a few issues. The initial data is invalid JSON, but this is an aside, I've fixed it in the data below.

You are looping over an empty array $lists which you create just before the loop. Here I've created this from the JSON data and removed the array initialisation. Lastly you where using $list['numbers'] where numbers isn't defined anywhere ...

$json = '[
  [
   {"id": 1, "name": "one"},
   {"id": 2, "name": "two"},
   {"id": 3, "name": "three"}
  ],
  [
   {"id": 4, "name": "four"},
   {"id": 5, "name": "five"},
   {"id": 6, "name": "six"}
  ]
]';

$lists = json_decode($json, true);
$numbers =[];
foreach ($lists as $list) {
    $numbers = array_merge($numbers, $list);
}
print_r($numbers);

Update:

As a guess to what you've added in the comments to the question, try...

$numbers =[];
foreach ($lists['numbers'] as $list) {
    $numbers = array_merge($numbers, $list);
}
print_r($numbers);

6 Comments

When I used array_merge, it says that the argument #2 is not an array.
How do you convert your data (I've used json_decode()) to the data you want to use. I can't test it if I don't have your original data.
I've added a new piece of code, this may be what your asking for but it isn't clear.
question edited, please see and mybe it can help you identify my problem
@MONSTER The $lists is not a corrcet php array, meybe it's an output from APi or dumping something in your controller. please be sure what is $lists type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.