【问题标题】:Array fetching all data in the last loop数组在最后一个循环中获取所有数据
【发布时间】:2019-01-28 16:12:27
【问题描述】:

我已经编写了代码来根据类别获取播客的数据并获取 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"
      }      
    ]
  }
]

【问题讨论】:

  • 请添加一个你得到的和你期望得到的样本 json 输出。

标签: php arrays json


【解决方案1】:

您将所有类别组合在一起,因为您的 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] => [...]
]

【讨论】:

  • 感谢 @line88 的帮助,但我也用 $output 填充了 $json 数组,但它没有给出结果我的实际错误在哪里?
  • @Alisha 嘿,你能创建一个php sandbox,示例。保存并在此处分享链接,这样我就很容易得到你愿意做什么。只需用虚拟数组数据填充它。
猜你喜欢
  • 2020-12-01
  • 2021-11-13
  • 2018-08-17
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 2020-07-30
  • 2021-01-20
  • 1970-01-01
相关资源
最近更新 更多