这是我试图迭代的以下JSON结构
{
"AS":{
"Query":"vi",
"FullResults":1,
"Results":[
{
"Type":"AS",
"Suggests":[
{
"Txt":"videos",
"Type":"AS",
"Sk":""
},
{
"Txt":"vipjatt",
"Type":"AS",
"Sk":"AS1"
},
{
"Txt":"vit",
"Type":"AS",
"Sk":"AS2"
},
{
"Txt":"vijaya bank",
"Type":"AS",
"Sk":"AS3"
},
{
"Txt":"videocon d2h",
"Type":"AS",
"Sk":"AS4"
},
{
"Txt":"visarev",
"Type":"AS",
"Sk":"AS5"
},
{
"Txt":"vijaya karnataka",
"Type":"AS",
"Sk":"AS6"
},
{
"Txt":"video songs",
"Type":"AS",
"Sk":"AS7"
}
]
}
]
}
}
这是我用来迭代它的代码,并尝试访问“Txt”属性
$data = $info->get($url);
$content = json_decode($data);
$i = 0;
foreach($content->AS->Results as $item) {
$each = $item->Suggests[$i]->Txt;
$i++;
echo $each;
}
但我只能访问“Txt”属性的第一个外观。 我的代码有什么问题?为什么不打印“Txt”属性的每个外观??
答案 0 :(得分:3)
您正在迭代AS->Results
项,其中只有一项。您想直接迭代Suggests
数组:
foreach ($content->AS->Results[0]->Suggests as $item) {
echo $item->Txt;
}
我不知道Results
数组中是否有多个项目。也许您需要首先迭代它们以选择正确的Suggests
项来迭代。