我正在尝试使用 PHP 解析 JSON,但我遇到了麻烦,因为我的数组是多维的,而且我不一定知道我要查找的所有内容的名称。我想要做的是获取 JSON 文件中每个项目的 ID。我在这里查看过,但我发现的解决方案仅适用于密钥的一个实例,或者要求您知道密钥的值。为了向您展示我想要的示例,这是我的 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。这是我的代码。
<?php
$content = json_decode($json);
foreach($content->products_and_categories->new as $entry){
echo $entry->id;
}
它返回这个:
1950
1951
1947
那么如何返回所有其他类别的 ID?谢谢您阅读此篇。