【问题标题】:php multidimensional array to string or tablephp多维数组到字符串或表
【发布时间】:2019-02-06 04:58:10
【问题描述】:

如何将此数组输出到 html 表中? 对于每一行,我想在 foreach 中像这样输出它;

echo "<td>".$lat."</td><td>".$long."</td>";

根据https://developer.here.com/documentation/routing-waypoints/topics/quick-start-simple-car.html 上的示例

我已经尝试过代码

$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE"; 
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true); 
foreach($api_response_decoded as $api_response_decoded_row){ 
    print_r($api_response_decoded_row[0][waypoints]); 
} 

也试过了

print_r($api_response_decoded_row[0][waypoints][id]);  

也试过了

echo($api_response_decoded_row[0][waypoints][id]);  

也试过了

implode($api_response_decoded_row[0][waypoints][id]);  

【问题讨论】:

  • 请帮我改进这个问题 - 为什么要投反对票?我用谷歌搜索了它,花了大约 2 个小时从 stackoverflow 和其他网站尝试各种事情 - 我当然尝试自己先解决它!
  • 你在哪里尝试在 html 表中输出数组?你有一个json,但你只使用print_rechoimplode,html结构在哪里?你能告诉我们你从这 3 种方式中得到了什么以及你想要什么吗?
  • 对于每一行 echo "&lt;td&gt;".$lat."&lt;/td&gt;&lt;td&gt;".$long."&lt;/td&gt;"; 见编辑,这说明清楚了吗?
  • 你试过$api_response_decoded_row['results']['waypoints']吗?
  • 另外,感谢投反对票的人删除:-)。

标签: php html arrays json multidimensional-array


【解决方案1】:

感谢评论者和回答者。因此,如果它对其他人有帮助,那么完整的工作代码就是;

$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE"; 
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true); 
echo "<table>";
foreach($api_response_decoded as $api_response_decoded_rows){ 
  foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
    $html = '<tr><td>'.$waypoint['sequence'].'</td><td>'.$waypoint['id'].'</td><td>'.$waypoint['lat'].'</td><td>'.$waypoint['lng'].'</td></tr>';
    echo $html;
  }
} 
echo "</table>";

【讨论】:

    【解决方案2】:

    如果 cmets 还没有为您提供足够的帮助,这是您可以做到的一种方法。

    foreach($api_response_decoded as $api_response_decoded_rows){ 
    
      foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
    
        $html = '
                <td>'.$waypoint['lat'].'</td>
                <td>'.$waypoint['lng'].'</td>
                ';
    
        echo $html;
      }
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多