user3724476
user3724476

Reputation: 5130

Find highest value entry in sub array using JSON & PHP

I have been playing around with this for a few hours now and have had not much luck.

My current JSON looks like this: https://pastebin.com/TSmWFA2g

"10-10-2019 12:00AM":[ 
  { 
     "speed":33,
     "latitude":-11.2588112,
     "longitude":100.8249533
  },
  { 
     "speed":33,
     "latitude":-11.2381112,
     "longitude":100.82509
  },
  { 
     "speed":31,
     "latitude":-11.827312,
     "longitude":100.8242733
  }
],
"10-10-2019 12:01AM":[ 
  { 
     "speed":29,
     "latitude":-11.2902112,
     "longitude":100.8202849
  },
  { 
     "speed":26,
     "latitude":-11.2826432,
     "longitude":100.3760333
  }
]

What I am attempting to do is for each date find the entry that has the highest "speed" and remove the other entries under that date (or create a new array with the single entry).

EDIT 01: I have now tried:

function my_sort($a,$b)
{
return $b['speed'] - $a['speed'];
}
usort($finalData,"my_sort");
echo json_encode($finalData);

This sorts the data by speed but the JSON now does not include the date found in the original JSON.

[{"speed":33,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":33,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":31,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":31,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":33,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":32,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":24,"latitude":-11.2588112,"longitude":100.82509}, 
{"speed":16,"latitude":-11.2588112,"longitude":100.82509},]

Upvotes: 0

Views: 84

Answers (3)

Nigel Ren
Nigel Ren

Reputation: 57121

As you just want the highest for each date, this code just loops round each item and stores the one with the highest speed for the date, if there are multiple ones with the same speed, the first is taken. This saves having to sort the arrays and then chop them up and makes 1 pass through the data...

$output = [];
$input = json_decode($data, true);
foreach ( $input as $date => $dateSection )  {
    $max = ["speed" => 0];
    foreach ( $dateSection as $item )   {
        if ( $item["speed"] > $max["speed"] )   {
            $max = $item;
        }
    }
    $output[$date] = $max;
}
print_r($output);

this gives the output...

Array
(
    [10-10-2019 12:00AM] => Array
        (
            [speed] => 33
            [latitude] => -11.2588112
            [longitude] => 100.8249533
        )

    [10-10-2019 12:01AM] => Array
        (
            [speed] => 29
            [latitude] => -11.2902112
            [longitude] => 100.8202849
        )

    [10-10-2019 12:02AM] => Array
        (
            [speed] => 35
            [latitude] => -11.2991112
            [longitude] => 100.0129199
        )

)

Upvotes: 0

Casper
Casper

Reputation: 1539

$max = []; //store highest speeds in new array
foreach ($json as $key => $obj) {
   $max[$key] = max(array_map(function($o) {
      return $o;
  }, $obj));
}

Working sample: http://sandbox.onlinephpfunctions.com/code/2f6e1a86775e206650bfe86f7602464c0fce17f0

Upvotes: 0

Mukesh Yadav
Mukesh Yadav

Reputation: 101

<?php

$data = json_decode('{
   "10-10-2019 12:00AM":[
      {
         "speed":33,
         "latitude":-11.2588112,
         "longitude":100.8249533
      },
      {
         "speed":33,
         "latitude":-11.2381112,
         "longitude":100.82509
      },
      {
         "speed":31,
         "latitude":-11.827312,
         "longitude":100.8242733
      }
   ],
   "10-10-2019 12:01AM":[
      {
         "speed":29,
         "latitude":-11.2902112,
         "longitude":100.8202849
      },
      {
         "speed":26,
         "latitude":-11.2826432,
         "longitude":100.3760333
      }
   ],
   "10-10-2019 12:02AM":[
      {
         "speed":35,
         "latitude":-11.2991112,
         "longitude":100.0129199
      },
      {
         "speed":33,
         "latitude":-11.9273112,
         "longitude":100.8734016
      },
      {
         "speed":32,
         "latitude":-11.2533212,
         "longitude":100.19229
      },
      {
         "speed":30,
         "latitude":-11.2928112,
         "longitude":100.2495099
      },
      {
         "speed":24,
         "latitude":-11.2228112,
         "longitude":100.9266033
      }
   ]
}',true);

$newArray=array();
foreach ($data as $key => $value) {
    array_multisort(array_column($value, 'speed'), SORT_DESC, $value);
    $newArray[$key]=$value[0];
}

echo "<pre>";
print_r($newArray);
echo "<pre>";
?>

Upvotes: 1

Related Questions