0

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},]
user3724476
  • 3,599
  • 3
  • 10
  • 16
  • Sort each array by speed, then use `splice()` to remove all but the first element. – Barmar Oct 14 '19 at 09:04
  • @Barmar I'm quite new to JSON with PHP, could you give an example of how I would sort the array by speed? – user3724476 Oct 14 '19 at 09:15
  • StackOverflow is not a free coding service. You're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Please update your question to show what you have already tried in a [mcve]. For further information, please see [ask], and take the [tour] :) – Barmar Oct 14 '19 at 09:16
  • Use `json_decode()` to turn it into a PHP array. Then use `usort()` to sort each array. Then use `array_splice()` to remove all the other entries from the array. – Barmar Oct 14 '19 at 09:17
  • @Barmar I will give that a try and in the future update the question. I am aware this isn't a "free coding service". As I stated at the start of the question I have been going at this for 3 hours now. It took me a while to even come to having an array like that, which I did not state originally. I had exhausted all my abilities before coming here to ask for help. – user3724476 Oct 14 '19 at 09:24
  • Show what you tried, and we'll help you fix it. We just won't write it from scratch. – Barmar Oct 14 '19 at 09:25
  • I've tried the below, it look's like it's now being sorted from 0 but it has removed the dates. `function my_sort($a,$b) { if ($a==$b) return 0; return ($a – user3724476 Oct 14 '19 at 09:32
  • Put the code in the question – Barmar Oct 14 '19 at 09:33
  • You're not comparing the speeds in `my_sort`. – Barmar Oct 14 '19 at 09:33
  • It should be `function my_sort($a, $b) { return $b['speed'] - $a['speed']; }` – Barmar Oct 14 '19 at 09:34
  • See https://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php – Barmar Oct 14 '19 at 09:35

3 Answers3

1
<?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>";
?>
0
$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

Casper
  • 1,250
  • 9
  • 15
0

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
        )

)
Nigel Ren
  • 51,875
  • 11
  • 34
  • 49