我有以下JSON数据,我试图解析这些类别:
{
"skimlinksProductAPI": {
"status": 200,
"message": "OK",
"version": 3,
"categories": {
"1": "Animals",
"2": "Animals > Live Animals",
"3": "Animals > Pet Supplies",
"4": "Animals > Pet Supplies > Bird Supplies",
"5": "Animals > Pet Supplies > Bird Supplies > Bird Cages & Stands",
"6": "Animals > Pet Supplies > Bird Supplies > Bird Food",
"7": "Animals > Pet Supplies > Bird Supplies > Bird Ladders & Perches",
"8": "Animals > Pet Supplies > Bird Supplies > Bird Toys",
...
}
}
}
我尝试了以下但不能正常工作:
$catList = json_decode(file_get_contents('http://api-product.skimlinks.com/categories?key=MY_KEY&format=json'),true);
$catList=$catList['skimlinksProductAPI']['categories'];
foreach ($catList as $element){
echo $element[0].' - '.$element[1];
}
答案 0 :(得分:1)
categories
的元素不是子数组,它们只是键值对。 foreach
应为:
foreach ($catList as $id => $element){
echo $id . ' - ' . $element;
}