PHP多维数组到字符串或表

时间:2018-08-31 07:40:47

标签: php html arrays json multidimensional-array

如何将该数组输出到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 个答案:

答案 0 :(得分:2)

如果评论对您的帮助不够,那么您可以采用这种方法。

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

} 

答案 1 :(得分:0)

感谢评论者和回答者。万一它对别人有帮助,那么完整的工作代码就可以了;

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