繁体   English   中英

根据字符串值对数组项进行分组

[英]Grouping array items based on string value

我正在尝试按sport对数组进行分组。 可能是n个运动。 最后,然后使用它创建一个新数组。 是否有一种有效的方法而不会过度杀伤力?

$sports = [
    ['sport' => 'soccer', 'id' => 97487];
    ['sport' => 'soccer', 'id' => 244800];
    ['sport' => 'soccer', 'id' => 258740];
    ['sport' => 'basketball', 'id' => 147884];
    ['sport' => 'baseball', 'id' => 222240];
    ['sport' => 'baseball', 'id' => 222245];
];

初始数组:

array(6) {
  [0]=>
  array(2) {
    ["sport"]=>
    string(6) "soccer"
    ["id"]=>
    int(97487)
  }
  [1]=>
  array(2) {
    ["sport"]=>
    string(6) "soccer"
    ["id"]=>
    int(244800)
  }
  [2]=>
  array(2) {
    ["sport"]=>
    string(6) "soccer"
    ["id"]=>
    int(258740)
  }
  [3]=>
  array(2) {
    ["sport"]=>
    string(10) "basketball"
    ["id"]=>
    int(147884)
  }
  [4]=>
  array(2) {
    ["sport"]=>
    string(8) "baseball"
    ["id"]=>
    int(222240)
  }
  [5]=>
  array(2) {
    ["sport"]=>
    string(8) "baseball"
    ["id"]=>
    int(222245)
  }
}

所需结果:

array(3) 
{
    [0]=>
    array(3) {
        [0]=>
        array(2) {
          ["sport"]=>
          string(6) "soccer"
          ["id"]=>
          int(97487)
        }
        [1]=>
        array(2) {
          ["sport"]=>
          string(6) "soccer"
          ["id"]=>
          int(244800)
        }
        [2]=>
        array(2) {
          ["sport"]=>
          string(6) "soccer"
          ["id"]=>
          int(258740)
        }
    }
    [1]=> 
        array(1) {
        [0]=>
        array(2) {
          ["sport"]=>
          string(10) "basketball"
          ["id"]=>
          int(147884)
        }
    }
    [2]=> 
    array(2) {
        [0]=>
        array(2) {
          ["sport"]=>
          string(8) "baseball"
          ["id"]=>
          int(222240)
        }
        [1]=>
        array(2) {
          ["sport"]=>
          string(8) "baseball"
          ["id"]=>
          int(222245)
        }
    }
}

您可以像这样对数组进行分组:

$sports = [
    ['sport' => 'soccer', 'id' => 97487],
    ['sport' => 'soccer', 'id' => 244800],
    ['sport' => 'soccer', 'id' => 258740],
    ['sport' => 'basketball', 'id' => 147884],
    ['sport' => 'baseball', 'id' => 222240],
    ['sport' => 'baseball', 'id' => 222245]
];

// we will build an array here where the key is the sport
// name and the value is an array of objects pertaining
// to that sport i.e. 'basketball' => [bb1, bb2, ...]

$array = array();

// now consider every sport object in your original array

foreach($sports as $key => $item)
{
   if (array_key_exists($item['sport'], $array)) {
     // we encountered this same sport in the past so we
     // know $array['sportName'] already exists and can
     // push right to it
     $array[$item['sport']][] = $item;
   } else {
     // we have never seen this sport before and now must
     // insert the sport into $array['sportName'] = []
     // and push this sport object to it
     $array[$item['sport']] = [$item];
   }
}

// since $array's keys are the names of the sports themselves, but
// you want the keys to be numeric, this will build a new array
// from just the values of $array which at this point contains
// grouped arrays of sport objects

$result = array_values($array);

// print the results for good measure :)

print_r($result);

这可以通过循环运动数组并构建第二个[sportName => arrayOfThatSport]数组来[sportName => arrayOfThatSport] 在for循环中,我们正在检查给定运动是否已存在于此数组中。 如果可以,那么很好,将该运动添加到该运动的相应数组中。 否则,请通过$array['sportName'] = [sportObject]为该运动创建一个新数组。 如果考虑循环的几次迭代,您会发现我们要么总是添加到现有的$array['sportName']数组中,要么每遇到一个从未有过的新运动,就在这个位置创建一个新数组。 这为我们提供了一个类似于以下的最终数组: [ "sport : [...], "sport2": [...] ]但在您的情况下,您需要一个数字数组而不是一个关联数组,因此需要调用array_values

这是我建议的解决方案。

$groupedSports = [];
foreach ($sports as $item) {
    if (empty($groupedSports[$item['sport']])) {
        $groupedSports[$item['sport']] = [];
    }
    $groupedSports[$item['sport']][] = $item;
}

简短说明:

  • 第1行:我们初始化一个空数组
  • 第2行:我们在原始数组上开始循环
  • 第3-5行:如果这是当前运动的首次出现,我们会为该运动初始化一个空数组(避免警告)
  • 第6行:我们将当前项目分配给适当的数组

暂无
暂无

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

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