Get the latest json data in php

0

Galera follows the code below that lists all. I need only the last given latitude and longitude

$conteudo = json_decode(file_get_contents('https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/0ozWRQqxiMnv5bqJzSUIMyUIIbMGrP5qu/message.json'));
foreach ($conteudo->response->feedMessageResponse->messages->message as $key) {
    print_r('Latitude: '.$key->latitude);
    echo ' - ';
    print_r('longitude: '.$key->longitude);
    echo '<br>';
}
    
asked by anonymous 03.02.2018 / 20:59

1 answer

0

We can use the end() function that takes the last element of an array. But we have an object, so before we have to convert this object to array. To do this, simply pass a second parameter to the json_decode() function as true . Done that we can get the last value like this:

$ultimo = end($conteudo['response']['feedMessageResponse']['messages']['message']);

Now just print the value you want.

echo 'Latitude: '.$ultimo['latitude'];
echo ' - ';
echo 'longitude: '.$ultimo['longitude'];

Follow the complete code

<?php

    $conteudo = json_decode(file_get_contents('https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/0ozWRQqxiMnv5bqJzSUIMyUIIbMGrP5qu/message.json'),true);

    $ultimo = end($conteudo['response']['feedMessageResponse']['messages']['message']);

    echo 'Latitude: '.$ultimo['latitude'];
    echo ' - ';
    echo 'longitude: '.$ultimo['longitude'];    
?>
    
03.02.2018 / 21:41