如何使用多维数组的第3级值生成数组

时间:2017-05-19 05:48:04

标签: php arrays sorting multidimensional-array max

如下所示,我有一个多维数组,在第三级中有一个关联数组,我试图根据gamesCount进行操作。我希望在每项运动中生成一个新的最高 gamesCount的项目数组。如果没有过度杀伤,是否有一种有效的方法?

编辑:可能n次运动,因此目标是为每项运动找到gameCounts的最大数量。

初始数组:

$array = [
    [
        ["sport" => "soccer", "gamesCount" => 5, "gamesId" => 1],
        ["sport" => "soccer", "gamesCount" => 3, "gamesId" => 2],
        ["sport" => "soccer", "gamesCount" => 10, "gamesId" => 3],
        ["sport" => "soccer", "gamesCount" => 10, "gamesId" => 4],
    ],
    [
        ["sport" => "basketball", "gamesCount" => 1, "gamesId" => 5],
        ["sport" => "basketball", "gamesCount" => 3, "gamesId" => 6],
        ["sport" => "basketball", "gamesCount" => 3, "gamesId" => 7],
        ["sport" => "basketball", "gamesCount" => 8, "gamesId" => 8],
    ]
];

期望的结果:

array(3) {
      [0]=>
      array(3) {
        ["sport"]=>
        string(6) "soccer"
        ["gamesCount"]=>
        int(10)
        ["gamesId"]=>
        int(3)
      }
      [1]=>
      array(3) {
        ["sport"]=>
        string(6) "soccer"
        ["gamesCount"]=>
        int(10)
        ["gamesId"]=>
        int(4)
      }
      [2]=>
      array(3) {
        ["sport"]=>
        string(10) "basketball"
        ["gamesCount"]=>
        int(5)
        ["gamesId"]=>
        int(8)
      }
}

2 个答案:

答案 0 :(得分:1)

试试这个,查看live demo

$result = [];
foreach($array as $v)
{
$max = max(array_column($v, 'gamesCount'));
    $result = array_merge($result, array_filter($v, function($value)use($max){return $value['gamesCount'] == $max;}));
}
var_dump($result);

答案 1 :(得分:1)

输入:

$array=[
    [
        ["sport"=>"soccer","gamesCount"=>5,"gamesId"=>1],
        ["sport"=>"soccer","gamesCount"=>3,"gamesId"=>2],
        ["sport"=>"soccer","gamesCount"=>10,"gamesId"=>3],
        ["sport"=>"soccer","gamesCount"=>10,"gamesId"=>4]
    ],
    [
        ["sport"=>"basketball","gamesCount"=>1,"gamesId"=>5],
        ["sport"=>"basketball","gamesCount"=>3,"gamesId"=>6],
        ["sport"=>"basketball","gamesCount"=>3,"gamesId"=>7],
        ["sport"=>"basketball","gamesCount"=>8,"gamesId"=>8]
    ]
];

方法#1“带有两个循环的最容易阅读的版本”(Demo

foreach($array as $sport_array){
    $most_in_sport=max(array_column($sport_array,'gamesCount'));  // get highest gamesCount in sport's subarray      
    foreach($sport_array as $sport_row){
        if($sport_row['gamesCount']==$most_in_sport){  // only keep a sport's subarray (row) if its gamesCount is the highest in the sport
            $most_per_sport[]=$sport_row;
        }
    }
}
var_export($most_per_sport);

方法#2:“淫乱错综复杂的单行php minimum version: 5.6(对于那些不想担心单行天启的人 - - 参考:第一条评论@ https://stackoverflow.com/a/43950486/2943403。)(Demo

var_export(array_merge(...array_map(function($sport_array){$most_in_sport=max(array_column($sport_array,'gamesCount')); return array_filter($sport_array,function($sport_row)use($most_in_sport){return $sport_row['gamesCount']==$most_in_sport;});},$array)));

输出:

array (
  0 => 
  array (
    'sport' => 'soccer',
    'gamesCount' => 10,
    'gamesId' => 3,
  ),
  1 => 
  array (
    'sport' => 'soccer',
    'gamesCount' => 10,
    'gamesId' => 4,
  ),
  2 => 
  array (
    'sport' => 'basketball',
    'gamesCount' => 8,
    'gamesId' => 8,
  ),
)

决定使用哪种方法取决于每个程序员。第一种方法将是最有效的,IMO最容易理解。第二个将生成较少的全局变量,并且需要较少的代码行。如果你试图避免“ over-kill ”方法#1不能被打败。