在一行中打印 JSON 数据的问题

我有JSON数据,我需要在一行中打印类别,但类别具有多个值。


我尝试导出标签,但我坚持在一行中打印值。


打印文本中的示例:类别 1、类别2、类别 3、类别4"categories"


我尝试风箱,但不起作用:


foreach ($jf['data']['feed'] as $item1) {


    $item = $item1['article'];


 foreach($item['categories'] as $categories1) {

    echo $categories123;

 }

JSON 文件


{

  "status": "success",

  "data": {

    "feed": [

      {

        "article": {

          "id": "165354",

          "title": "Title1",

          "image_url": "example.com/image1.png",

          "native_app_url": "example.com/article1",

          "article_url": "example.com/article1",

          "article_type": "Article",

          "created_at": "2020-01-15T16:55:01Z",

          "featured_time": "2020-02-19T17:19:33Z",

          "author_name": "2",

          "matches": [


          ],

          "categories": [

            "Categories1",

            "Categories2",

            "Categories3",

            "Categories4"

          ],

          "tags": [

            "Tags1",

            "Tags2",

            "Tags3"

          ],

          "embed_code": "<embed>12453</embed>",

          "synd_code": "<embed>121245</embed>"

        }

      },

      {

        "article": {

          "id": "112354",

          "title": "Title2",

          "image_url": "example.com/image2.png",

          "native_app_url": "example.com/article2",

          "article_url": "example.com/article2",

          "article_type": "Article",

          "created_at": "2020-01-11T16:55:01Z",

          "featured_time": "2020-01-19T17:19:33Z",

          "author_name": "1",

          "matches": [


          ],

          "categories": [

            "Categories1",

            "Categories2",

            "Categories3",

            "Categories4"

          ],

          "tags": [

            "Tags1",

            "Tags2",

            "Tags3"

          ],

          "embed_code": "<embed>123</embed>",

          "synd_code": "<embed>12345</embed>"

        }

      },

      {

        "article": {

        ...

        }

      }

    ],

    "next_page": 2

  },

  "message": ""


开心每一天1111
浏览 99回答 1
1回答

胡说叔叔

用于在数组元素之间插入分隔符。implode$categories = implode(", ", $item['categories']);如果只想要一个有限的数量,请在内爆之前对数组进行切片。$category_limit = 4; // only show first 4 categories$categories = implode(", ", array_slice($item['categories'], 0, $category_limit));
打开App,查看更多内容
随时随地看视频慕课网APP