Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello how can i select the latest value of an array. I have build an code but it shows all the entries in the array. i need only the latest.

<pre>$readjson = file_get_contents('employees.json') ;

//Decode JSON
$data = json_decode($readjson, true);




//Parse the employee name
foreach($data[0]['results'] as $result){
  echo $result['dec_lat']."<br/>";
} 


What I have tried:

Searched in the Internet but I think with wrong Keyword
Posted
Updated 1-Jul-20 11:19am
Comments
Fynn Pfingsten 1-Jul-20 12:03pm    
The Array looks like:

[
   {
      "results":[
         {
            "signal_id":11328555834,
            "dec_lat":52,
            "dec_long":8


},
         {
            "signal_id":11328549683,
            "dec_lat":53,
            "dec_long":9


},
         {
            "signal_id":11328549682,
            "dec_lat":12,
            "dec_long":6


}


]


}


And i Want the first "dec_lat" of the array.

foreach is a loop, which runs through each of the elements in the array ion turn - which is why it prints all the elements.

If you want to only print the last element, then only use the last index to access the last element.

This may help: PHP: array_key_last - Manual[^]
 
Share this answer
 
The Array must be:

HTML
{
      "results":[
         {
            "signal_id":11328555834,
            "dec_lat":52,
            "dec_long":8
         },
         {
            "signal_id":11328549683,
            "dec_lat":53,
            "dec_long":9
         },
         {
            "signal_id":11328549682,
            "dec_lat":12,
            "dec_long":6
         }
     ]
}


$dec_lat = $data["results"][count($data["results"])-1]["dec_lat"];
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900