PHP解析JSON以获取多维数组中键的值

问题描述:

我正在尝试使用PHP解析JSON,但由于数组是多维的,因此遇到了麻烦,我不一定知道要查找的所有内容的名称.我想做的就是获取JSON文件中每个项目的ID.我在这里查看过,但是我发现的解决方案仅适用于某个密钥实例,或者要求您知道密钥的值.为了向您展示我想要的示例,这是我的JSON.

I'm trying to parse JSON with PHP, but I'm having trouble as my array is multidimensional and I won't necessarily know the names of everything I'm looking for. What I want to do is get the ID of every single item in the JSON file. I've looked on here but the solutions I've found only work for one instance of a key or require you to know the key's value. To show you an example of what I want, here's my JSON.

{
   "products_and_categories":{
      "new":[
         {
            "category_name":"Jacket",
            "name":"Half Zip Windbreaker",
            "position":3,
            "sale_price":0,
            "image_url":"0/65030/Half_Zip_Windbreaker_Green_1365029293_cart_1365029294.jpg",
            "price":14800,
            "new_item":true,
            "detail_view_url":null,
            "id":1950
         },
         {
            "category_name":"Jacket",
            "name":"The North Face\u00ae/Supreme\u003Cbr\u003EReflective 3M\u00ae Mountain Parka",
            "position":0,
            "sale_price":0,
            "image_url":"0/65059/The_North_Face--r----s--Supreme_br_Reflective_3M--r--_Mountain_Parka_Red_1365029303_cart_1365029305.jpg",
            "price":49800,
            "new_item":true,
            "detail_view_url":null,
            "id":1951
         },
         {
            "category_name":"Hats",
            "name":"Rubber Logo Camp Cap",
            "position":29,
            "sale_price":0,
            "image_url":"0/64915/Rubber_Logo_Camp_Cap_Navy_1365029249_cart_1365029251.jpg",
            "price":4800,
            "new_item":true,
            "detail_view_url":null,
            "id":1947
         }
      ],
      "Hats":[
         {
            "category_name":"Hats",
            "name":"Rubber Logo Camp Cap",
            "position":29,
            "sale_price":0,
            "image_url":"0/64915/Rubber_Logo_Camp_Cap_Navy_1365029249_cart_1365029251.jpg",
            "price":4800,
            "new_item":true,
            "detail_view_url":null,
            "id":1947
         },
         {
            "category_name":"Hats",
            "name":"Enamel S 5-Panel",
            "position":34,
            "sale_price":0,
            "image_url":"0/63614/Enamel_S_5-Panel_Green_1362616033_cart_1362616034.jpg",
            "price":5400,
            "new_item":false,
            "detail_view_url":null,
            "id":1904
         },
         {
            "category_name":"Hats",
            "name":"Big Striped Beanie",
            "position":35,
            "sale_price":0,
            "image_url":"0/61745/Big_Striped_Beanie_Green_1362023179_cart_1362023181.jpg",
            "price":3200,
            "new_item":false,
            "detail_view_url":null,
            "id":1853
         }
      ],
      "Decks":[
         {
            "category_name":"Decks",
            "name":"Power, Corruption, Lies Cruiser",
            "position":46,
            "sale_price":0,
            "image_url":"0/62026/Power__Corruption__Lies_Cruiser_Black_1362023285_cart_1362023286.jpg",
            "price":6000,
            "new_item":false,
            "detail_view_url":null,
            "id":1861
         },
         {
            "category_name":"Decks",
            "name":"Bling Logo Deck",
            "position":47,
            "sale_price":0,
            "image_url":"0/62018/Bling_Logo_Deck_Platinum_1362023282_cart_1362023283.jpg",
            "price":4900,
            "new_item":false,
            "detail_view_url":null,
            "id":1860
         }
      ],
      "Sweatshirts":[
         {
            "category_name":"Sweatshirts",
            "name":"Heathered Crewneck",
            "position":11,
            "sale_price":0,
            "image_url":"0/63855/Heathered_Crewneck_Heather_Pink_1362616124_cart_1362616125.jpg",
            "price":11800,
            "new_item":false,
            "detail_view_url":null,
            "id":1914
         },
         {
            "category_name":"Sweatshirts",
            "name":"Checkered Pullover",
            "position":12,
            "sale_price":0,
            "image_url":"0/63826/Checkered_Pullover_Yellow_1362616113_cart_1362616114.jpg",
            "price":14800,
            "new_item":false,
            "detail_view_url":null,
            "id":1913
         }
      ]
   },
   "unique_detail_view_url_prefixes":[

   ],
   "sales_available":false,
   "unique_image_url_prefixes":[
      "http://d2flb1n945r21v.cloudfront.net/production/uploaded/style/"
   ]
}

现在,如果我只想要新类别中的项目ID,我的代码就可以工作,但是我想要实现的是获取整个JSON文件中每个项目的ID. 这是我的代码.

Now, my code works if I just want the ID of items in the new category, but what I want to achieve is to get the ID of every item in the whole JSON file. Here's my code.

<?php
$content = json_decode($json);
foreach($content->products_and_categories->new as $entry){
    echo $entry->id;
}

它返回此:

1950
1951
1947

那么,如何返回所有其他类别的ID?谢谢您阅读.

So how do I return the IDs for all of the other categories? Thank you for reading this.

您只需要通过遍历每个类别以及这些类别中的每个产品,就可以稍微改变循环的方法.

You just need to change your approach to looping slightly by iterating over each of the categories and each of the products within those categories.

foreach($content->products_and_categories as $category_name => $products) {
    echo $category_name;
    foreach ($products as $product) {
        echo $product->id;
    }
}