我正在尝试从数组中获取JSON,但在输出中得到了一个额外的方括号。
我尝试使用$episode[0] = $podcast->getPodcastByCategoryId($id);
,但是它只给出不完整的输出,意味着它给出了数组中第一次迭代的数据。而且,如果我从$ episode中删除[]
,它也会给出第一次迭代的数据。
$podList = $category->getCategoryPodcast();
if ($podList) {
foreach ($podList as $items) {
$id = $items->id;
$episode[] = $podcast->getPodcastByCategoryId($id);
}
}
echo json_encode($episode);
我希望只有一个方括号,但是我得到的输出带有两个方括号。
这是我从上面的代码中收到的信息:
[
[
{
"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"
}
]
]
根据@ splash58和@Vinesh Goyal的建议,我得到这样的输出:
[[
{
"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"
}
]
但是,第一个数据的方括号呢?
答案 0 :(得分:1)
您可以在获取时合并数组
$episode = array_merge($episode, $podcast->getPodcastByCategoryId($id));
或者在转换为json之前将结果数组展平
echo json_encode(array_merge(...$episode));
答案 1 :(得分:0)
使用javascript .concat()
跟随以下代码段。这符合您想要的输出吗?
var data = [[
{
"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"
}
]
];
var mergedData = data[0].concat(data[1]);
console.log(mergedData)
答案 2 :(得分:0)
根据您的代码,$podcast->getPodcastByCategoryId($id);
返回一个数组,以便t0消除您可以使用array_merge
函数的问题
$podList = $category->getCategoryPodcast();
$episode = [];
if ($podList) {
foreach ($podList as $items) {
$id = $items->id;
$episode = array_merge($episode, $podcast->getPodcastByCategoryId($id)
}
}
echo json_encode($episode);
我希望它会对您有所帮助:)