Skip to content
Advertisement

How do I remove the extra square bracket from JSON having multiple arrays

I am trying to get JSON from an array but I am getting an extra square bracket in the output.

I tried using $episode[0] = $podcast->getPodcastByCategoryId($id); but it only gives the incomplete output means it gives data of the first iteration from the array. And if I remove [] from $episode that also gives the data of first iteration.

$podList = $category->getCategoryPodcast();

if ($podList) {

    foreach ($podList as $items) {

        $id = $items->id;

        $episode[] = $podcast->getPodcastByCategoryId($id); 

    } 

}

echo json_encode($episode);

I expect to have only one square bracket but I am getting the output with two square brackets.

This is the out I am receiving from the above code:

[
[
    {
      "id": "6",
      "title": "Types of loops in PHP.",
      "description": "s.kfjhsdlufdgf o",
      "duration": "0:05:05",
      "audio": "http://demo.web/uploads/podAudio/PodAudio-2019011511270942.mp3",
      "image": "http://demo.web/uploads/podImage/PodImage-20190115112709469.jpg",
      "category": "2",
      "added_date": "माघ १, २०७५",
      "category_title": "उमेर",
      "author": "John Doe",
      "episodes": "2"
    },
    {
      "id": "4",
      "title": "How to remove square brackets from JSON?",
      "description": "",
      "duration": "",
      "audio": "http://demo.web/uploads/podAudio/PodAudio-20190114111541297.mp3",
      "image": "http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg",
      "category": "2",
      "added_date": "पौष ३०, २०७५",
      "category_title": "उमेर",
      "author": "John Doe",
      "episodes": "2"
    }
  ],
  [
    {
      "id": "5",
      "title": "Hello World!",
      "description": "",
      "duration": "",
      "audio": "http://demo.web/uploads/podAudio/PodAudio-20190114111937115.mp3",
      "image": "http://demo.web/uploads/podImage/PodImage-20190114104302103.jpg",
      "category": "1",
      "added_date": "पौष ३०, २०७५",
      "category_title": "गृह पृष्ठ",
      "author": "John Doe",
      "episodes": "1"
    }
  ]
]

According to the recommendation from @splash58 and @Vinesh Goyal, I am getting output like this:

[[
{
  "id": "6",
  "title": "Types of loops in PHP.",
  "description": "s.kfjhsdlufdgf o",
  "duration": "0:05:05",
  "audio": "http://demo.web/uploads/podAudio/PodAudio-2019011511270942.mp3",
  "image": "http://demo.web/uploads/podImage/PodImage-20190115112709469.jpg",
  "category": "2",
  "added_date": "माघ १, २०७५",
  "category_title": "उमेर",
  "author": "John Doe",
  "episodes": "2"
},
{
  "id": "4",
  "title": "How to remove square brackets from JSON?",
  "description": "",
  "duration": "",
  "audio": "http://demo.web/uploads/podAudio/PodAudio-20190114111541297.mp3",
  "image": "http://demo.web/uploads/podImage/PodImage-20190114102432145.jpg",
  "category": "2",
  "added_date": "पौष ३०, २०७५",
  "category_title": "उमेर",
  "author": "John Doe",
  "episodes": "2"
}
  ],

    {
      "id": "5",
      "title": "Hello World!",
      "description": "",
      "duration": "",
      "audio": "http://demo.web/uploads/podAudio/PodAudio-20190114111937115.mp3",
      "image": "http://demo.web/uploads/podImage/PodImage-20190114104302103.jpg",
      "category": "1",
      "added_date": "पौष ३०, २०७५",
      "category_title": "गृह पृष्ठ",
      "author": "John Doe",
      "episodes": "1"
    }
]

but, what about the square brackets of first data?

Advertisement

Answer

You can merge arrays while getting

$episode = array_merge($episode, $podcast->getPodcastByCategoryId($id)); 

Or flatten the result array before converting to json

echo json_encode(array_merge(...$episode));

demo

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement