0

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]);  
9
  • please help me improve the question - why the downvote? I did google it and have spent about 2 hours trying various things from stackoverflow and other sites - I certainly tried to solve it myself first! Commented Aug 31, 2018 at 7:44
  • Where do you try to output the array in an html table? You have a json but you just use print_r, echo and implode, where is the html structure? Can you show us what you get from those 3 way and what you want? Commented Aug 31, 2018 at 7:46
  • for each row echo "<td>".$lat."</td><td>".$long."</td>"; see edit, does that clarify? Commented Aug 31, 2018 at 7:50
  • 1
    did you try $api_response_decoded_row['results']['waypoints'] ? Commented Aug 31, 2018 at 8:00
  • 1
    Also, thanks to whoever had downvoted, for removing :-). Commented Aug 31, 2018 at 8:01

2 Answers 2

2

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

} 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.