请查看:http://codepad.org/uNlMsvwj为json。
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
$i = 0;
foreach($json->response->results as $item){
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " "; echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/"; echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
$i++;
}
问题是只打印了第一个elemet。如果我用1手动更改$ i,我得到第二个。
我现在已经找了3个小时,但找不到解决方案。如果这是初学者的错误,请原谅我。
由于
更新 谢谢大家的快速帮助
答案 0 :(得分:1)
你的意思是像这样迭代结果吗?
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
此外,您可以简化echo
声明:
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo $item->entries[$i]->displayname.'<br>';
echo $item->entries[$i]->location->street.' ';
echo $item->entries[$i]->location->streetnumber.'<br>';
echo $item->entries[$i]->location->zipcode.' ';
echo $item->entries[$i]->location->city.'<br><br>';
echo $item->entries[$i]->phonenumbers[0]->area.'/';
echo $item->entries[$i]->phonenumbers[0]->number.'<br>';
}
}
答案 1 :(得分:0)
你应该像这样回应:
echo $item->entries[$i]->displayname . "<br>";
.
连接字符串。此外,echo
不使用任何()
。
答案 2 :(得分:0)
更改
foreach($json->response->results as $item){
到
foreach($json->response->results->entries as $item){
和这些行
echo($item->entries[$i]->displayname);echo "<br>";
到
echo $item->displayname . "<br>";
所以你的代码看起来像是:
foreach($json->response->results->entries as $item){
echo "{$item->displayname}<br />";
echo "{$item->location->street} {$item->location->streetnumber}<br />";
echo "{$item->zipcode}<br />";
echo "{$item->location->city}<br /><br />";
echo "{$item->phonenumbers[0]->area}/{$item->phonenumbers[0]->number}<br />";
}
答案 3 :(得分:0)
尝试for-loop而不是foreach:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}
...或在foreach中包含一个for循环:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results as $item){
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
答案 4 :(得分:0)
尝试这个,它可行我测试它:
foreach($json->response->results[0]->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}