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},]
10
  • Sort each array by speed, then use splice() to remove all but the first element. Commented Oct 14, 2019 at 9:04
  • @Barmar I'm quite new to JSON with PHP, could you give an example of how I would sort the array by speed? Commented Oct 14, 2019 at 9:15
  • 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. Commented Oct 14, 2019 at 9: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. Commented Oct 14, 2019 at 9:24
  • Show what you tried, and we'll help you fix it. We just won't write it from scratch. Commented Oct 14, 2019 at 9:25

3 Answers 3

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>";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

If you need result in JSON, Please use json_encode($newArray);
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

Comments

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.