OK here is my code, processing the JSON below it.
$json = file_get_contents('https://courier-api.danielpikilidis.com/track-one/elta/CP423172129GR');
$json_data = json_decode($json);
$last = $json_data;
echo $last->data->CP423172129GR->locations[0]->datetime."<br>";
echo $last->data->CP423172129GR->locations[0]->location."<br>";
echo $last->data->CP423172129GR->locations[0]->description."<br>";
and this is the JSON
{
"success": true,
"data": {
"CP423172129GR": {
"found": true,
"courier_name": "ELTA",
"courier_icon": "https://i.imgur.com/6FU9iW7.png",
"locations": [
{
"datetime": "2024-06-03T11:55:00+03:00",
"location": "ΣΟΛΩΝΟΣ (ΑΘ 56)",
"description": "Παραλαβή αποστολής"
},
{
"datetime": "2024-06-03T12:31:00+03:00",
"location": "ΣΟΛΩΝΟΣ (ΑΘ 56)",
"description": "Αποστολή βρίσκεται σε στάδιο μεταφοράς"
},
{
"datetime": "2024-06-03T23:58:00+03:00",
"location": "Κ.Δ. ΔΕΜΑΤΩΝ ΑΘΗΝΑΣ",
"description": "Άφιξη σε"
},
{
"datetime": "2024-06-04T01:58:00+03:00",
"location": "Κ.Δ. ΔΕΜΑΤΩΝ ΑΘΗΝΑΣ",
"description": "Αποστολή βρίσκεται σε στάδιο μεταφοράς"
}
],
"delivered": false
}
},
"error": {
"code": 200,
"message": ""
}
}
Question: How can I display the LAST nested values, which are dynamically updated? I tried with this:
echo $last->data->CP423172129GR->locations[-1]->location."<br>";
suggested by a post somewhere, but returns nothing.
………………….
New contributor
TheAsiawatcher is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.