Get the last json data in php

Asked

Viewed 333 times

0

Galera follows the code below that lists all. 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>';
}

1 answer

0


We can use the function end() that takes the last element of an array. However We have an object, so first we have to convert that object to array. To do this just pass a second parameter to the function json_decode() as true. Once this is done we can take the last value this way:

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

now just print the amount you want.

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

Follow the full 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'];    
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.