简体   繁体   English

使用 JSON 和 PHP 在子数组中查找最高值条目

[英]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我当前的 JSON 看起来像这样: 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:编辑 01:我现在尝试过:

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.这按速度对数据进行排序,但 JSON 现在不包括在原始 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},]
<?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>";
?>
$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工作样本: http://sandbox.onlinephpfunctions.com/code/2f6e1a86775e206650bfe86f7602464c0fce17f0

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...这样就不必对 arrays 进行排序,然后将它们切碎并通过数据进行 1 ...

$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...这给出了 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
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM