2017-05-19 53 views
0

我想按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) 
     } 
    } 
} 
+1

您应该尝试**自己编写代码**。在[**做更多的研究**之后](https://meta.stackoverflow.com/q/261592/1011527)如果你有问题**发布你已经尝试了**的**明确的解释不工作**并提供[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。阅读[如何问](http://stackoverflow.com/help/how-to-ask)一个很好的问题。请务必[参观](http://stackoverflow.com/tour)并阅读[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

@JayBlanchard我的道歉。我不知道从哪里开始。我会记住这一点以备将来参考。谢谢 – MaryCoding

回答

2

您可以将像这样的数组:

$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]。在for循环中,我们检查一下给定的运动是否已经存在于这个数组中。如果确实如此,那么将该运动添加到该运动的相应阵列中。否则,请在$array['sportName'] = [sportObject]处为该运动创建一个新阵列。如果您考虑循环的几次迭代,您会看到我们总是添加到现有的$array['sportName']阵列中,或者在遇到我们以前从未见过的新运动时在此位置创建新阵列。这给了我们最后一个数组,类似于:[ "sport : [...], "sport2": [...] ]但在你的情况下,你想要一个数组而不是关联数组,因此调用array_values

+0

完美的解决方案 – RomMer

+0

伟大的解释,确实是一个完美的解决方案 – MaryCoding

0

这是我建议的解决方案。

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

简短说明:

  • 线1:我们初始化一个空数组
  • 线2:我们开始最初的阵列上的循环
  • 线3-5:如果它是第一个对于当前运动,我们初始化一个空阵列(避免警告)
  • 第6行:我们将当前项分配给相应的阵列
+1

虽然这段代码可能会回答这个问题,但提供关于为什么和/或代码如何回答这个问题的附加上下文会提高它的长期价值。一个好的答案***将总是解释所做的事情以及为什么这样做,不仅是为了OP,还是为了将来访问SO。 –

+1

@JayBlanchard你是对的,但我只是从一个单调乏味的工作中休息一下,所以我只是喜欢自己:-)现在我将编辑我的问题。 –

相关问题