0

请查看: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++;
}

问题是只打印了第一个元素。如果我用 1 手动更改 $i 我得到第二个。

我现在一直在寻找 3 个小时,但找不到解决方案。如果是初学者的错误,请原谅我。

谢谢

更新: 谢谢大家的快速帮助

4

5 回答 5

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>';
  }
}
于 2013-09-12T21:53:15.823 回答
0

你应该像这样回显:

echo $item->entries[$i]->displayname . "<br>";

连接.字符串。也不echo使用任何().

于 2013-09-12T21:42:51.020 回答
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 />";
}
于 2013-09-12T21:43:00.367 回答
0

尝试使用 for 循环而不是 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>";
  }
}
于 2013-09-12T21:45:10.030 回答
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>";
}
于 2013-09-12T22:03:01.353 回答