我编写了代码来根据类别获取播客的数据并获取JSON数据,该代码似乎正常工作,但是在最后一个循环中,它为我提供了整个类别的数据,而不是最后一个类别ID。
$podList = $category->getCategoryPodcast();
$output = array();
$json = array();
if ($podList) {
foreach ($podList as $items) {
$id = $items->id;
$episode = $podcast->getPodcastByCategoryId($id);
$count = count($episode);
if (!empty($episode)) {
foreach ($episode as $episodes) {
$cat_id = $episodes->category;
$output['category_id'] = $cat_id;
$output['no. of episode'] = $count;
$output['podcast'][] = $episodes;
}
} $json[] = $output;
}
}
echo json_encode($json);
die;
我希望在最后一个循环中将特定类别的播客数据而不是整个类别的数据组合在一起。
我得到以下输出。
[
{
"category_id": "2",
"no. of episode": 2,
"podcast": [
{
"id": "6",
"title": "Advanced Javascript course",
"description": "s.kfjhsdlufdgf o",
"duration": "0:05:05",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-2019011511270942.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190115112709469.jpg",
"category": "2",
"added_date": "माघ १, २०७५",
"category_title": "उमेर",
"author": "John Doe"
},
{
"id": "4",
"title": "How to display array inside array?",
"description": "",
"duration": "",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-20190114111541297.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190114102432145.jpg",
"category": "2",
"added_date": "पौष ३०, २०७५",
"category_title": "उमेर",
"author": "John Doe"
}
]
},
{
"category_id": "1",
"no. of episode": 1,
"podcast": [
{
"id": "6",
"title": "Advanced Javascript course",
"description": "s.kfjhsdlufdgf o",
"duration": "0:05:05",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-2019011511270942.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190115112709469.jpg",
"category": "2",
"added_date": "माघ १, २०७५",
"category_title": "उमेर",
"author": "John Doe"
},
{
"id": "4",
"title": "How to display array inside array?",
"description": "",
"duration": "",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-20190114111541297.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190114102432145.jpg",
"category": "2",
"added_date": "पौष ३०, २०७५",
"category_title": "उमेर",
"author": "John Doe"
},
{
"id": "5",
"title": "How to get pretty URLs",
"description": "",
"duration": "",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-20190114111937115.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190114104302103.jpg",
"category": "1",
"added_date": "पौष ३०, २०७५",
"category_title": "गृह पृष्ठ",
"author": "John Doe"
}
]
}
]
但我希望仅获得一个数组作为category_id中的数组:1否。的情节数是1。
[
{
"category_id": "2",
"no. of episode": 2,
"podcast": [
{
"id": "6",
"title": "Advanced Javascript course",
"description": "s.kfjhsdlufdgf o",
"duration": "0:05:05",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-2019011511270942.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190115112709469.jpg",
"category": "2",
"added_date": "माघ १, २०७५",
"category_title": "उमेर",
"author": "John Doe"
},
{
"id": "4",
"title": "How to display array inside array?",
"description": "",
"duration": "",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-20190114111541297.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190114102432145.jpg",
"category": "2",
"added_date": "पौष ३०, २०७५",
"category_title": "उमेर",
"author": "John Doe"
}
]
},
{
"category_id": "1",
"no. of episode": 1,
"podcast": [
{
"id": "5",
"title": "How to get pretty URLs",
"description": "",
"duration": "",
"audio": "http://shikshak.web/uploads/podAudio/PodAudio-20190114111937115.mp3",
"image": "http://shikshak.web/uploads/podImage/PodImage-20190114104302103.jpg",
"category": "1",
"added_date": "पौष ३०, २०७५",
"category_title": "गृह पृष्ठ",
"author": "John Doe"
}
]
}
]
答案 0 :(得分:0)
您将所有类别组合在一起,因为output
数组是在循环外定义的,并且在每次循环迭代期间都会被填充。
要只用单个类别填充它,请在循环内创建它,因此在下一次迭代中,它将是一个空数组,准备用新数据填充
$podList = $category->getCategoryPodcast();
// $output = array();
$json = array();
if ($podList) {
foreach ($podList as $items) {
$output = array(); // Create output array here, to fill it with data.
$id = $items->id;
$episode = $podcast->getPodcastByCategoryId($id);
$count = count($episode);
if (!empty($episode)) {
foreach ($episode as $episodes) {
$cat_id = $episodes->category;
$output['category_id'] = $cat_id;
$output['no. of episode'] = $count;
$output['podcast'][] = $episodes;
}
}
$json[] = $output; // fill the $json array with outputs.
}
}
echo json_encode($json);
$ json输出将是这样
[
[0] => [
['category_id'] => 1
...
],
[1] => [...]
]