Lyndon Penson
Lyndon Penson

Reputation: 65

php multidimensional array to string or table

how can I output this array into html table? for each row I would like to output it like this, within the foreach;

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

as per example on https://developer.here.com/documentation/routing-waypoints/topics/quick-start-simple-car.html

I have tried the code

$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]); 
} 

and also tried

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

and also tried

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

and also tried

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

Upvotes: 0

Views: 77

Answers (2)

Lyndon Penson
Lyndon Penson

Reputation: 65

Thanks to commenters and answerers. In case it helps someone else, full working code is therefore;

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

Upvotes: 0

yasoh
yasoh

Reputation: 149

Here's one way you could do it if the comments didn't already help you enough.

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

} 

Upvotes: 2

Related Questions