【问题标题】:foreach in php iterates only through the first JSONphp 中的 foreach 仅遍历第一个 JSON
【发布时间】:2013-09-17 11:01:14
【问题描述】:

请查看: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 个小时,但找不到解决方案。如果是初学者的错误请见谅。

谢谢

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

【问题讨论】:

  • 你不需要 () 和 echo 语句。

标签: php json loops foreach


【解决方案1】:

你应该像这样回应:

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

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

【讨论】:

    【解决方案2】:

    改变

    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】:

      尝试使用 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>";
        }
      }
      

      【讨论】:

        【解决方案4】:

        您的意思是像这样迭代结果吗?

        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>';
          }
        }
        

        【讨论】:

          【解决方案5】:

          试试这个,我测试过它有效:

          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>";
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-04-03
            • 1970-01-01
            • 2020-04-26
            • 2019-12-08
            • 2018-06-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多