简体   繁体   中英

PHP Parsing JSON for value of a key in multidimensional array

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/"
   ]
}

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;
}

It returns this :

1950
1951
1947

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM