按列值对二维数组中的数据进行分组,并将原始行推入其专用组中

问题描述 投票:0回答:3

我有这个数组

array
  0 => 
    array

      'prodid' => string '2' (length=1)
      'qty' => int 2
      'price' => string '100.00' (length=6)
      'sid' => string '13' (length=2)
  2 => 
    array

      'prodid' => string '3' (length=1)
      'qty' => int 1
      'price' => string '380.00' (length=6)
      'sid' => string '13' (length=2)
  1 => 
    array

      'prodid' => string '8' (length=1)
      'qty' => int 1
      'price' => string '300.00' (length=6)
      'sid' => string '24' (length=2)

我怎样才能将它分成一个多维数组,其中值“sid”像这样匹配。

array
  0 => 
    array

      0 => 
        array
          'prodid' => string '2' (length=1)
          'qty' => int 2
          'price' => string '100.00' (length=6)
          'sid' => string '13' (length=2)
      1 => 
        array
          'prodid' => string '2' (length=1)
          'qty' => int 2
          'price' => string '100.00' (length=6)
          'sid' => string '13' (length=2)

  1 => 
    array

      0 => 
        array
          'prodid' => string '7' (length=1)
          'qty' => int 1
          'price' => string '200.00' (length=6)
          'sid' => string '15' (length=2)
php arrays multidimensional-array grouping sub-array
3个回答
2
投票

您选择要分组的值作为新数组的键,并将成员推送到其中

$new = array();
foreach($array as $member)
{
    $key = $member['group-value'];
    $new[$key][] = $member;
}

然后,您可以使用

array_values
:

从新数组中提取裸值
$new = array_values($new);

就是这样。如果你想节省最后一部分,你也可以自己创建键的映射及其数字索引:

$new = array();
$keys = array();
foreach($array as $member)
{
    $key = $member['group-value'];
    isset($keys[$key]) || $keys[$key] = count($keys);        
    $new[$keys[$key]][] = $member;
}

2
投票

适用于 PHP5.3+

$result = array_reduce (
  $array,
  function ($item, $result) {
    if (!isset($result[$item['sid']])) $result[$item['sid']] = array();
    $result[$item['sid']][] = $item;
    return $result;
  },
  array()
)

1
投票
// $ouput array will be indexed by the 'sid' value
$output = array();

// Iterate over the main array and create a new subarray if 
// it doesn't already exist, or add to it if it does.
foreach($input as $subarr) {
  if (!isset($output[$subarr['sid']]) {
    // New array indexed by sid
    $output[$subarr['sid']] = array();
  }
  // Append the whole array
  $output[$subarr['sid']][] = $subarr;
}

注意:这会产生与您描述的稍有不同的输出,因为它使用

sid
作为数组键,而不是仅仅从 0:

开始索引它们
array
  // Note key difference...
  // sid == 13
  13 => 
    array
      0 => 
        array
          'prodid' => string '2' (length=1)
          'qty' => int 2
          'price' => string '100.00' (length=6)
          'sid' => string '13' (length=2)
      1 => 
        array
          'prodid' => string '2' (length=1)
          'qty' => int 2
          'price' => string '100.00' (length=6)
          'sid' => string '13' (length=2)
  // sid == 15
  15 => 
    array

      0 => 
        array
          'prodid' => string '7' (length=1)
          'qty' => int 1
          'price' => string '200.00' (length=6)
          'sid' => string '15' (length=2)
© www.soinside.com 2019 - 2024. All rights reserved.