按三列对二维数组中的数据进行分层分组

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

我有一个 PHP 数组:

array (size=xxx)
  0 => 
    array (size=9)
      'cat' => string 'FIRST CAT'
      'dur' => string '10'
      'type' => string 'Type description 10'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 10-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  1 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '01:00'
      'end' => string '02:00'
      'desc' => string 'Event description 10-2'
      'price' => string ''
      'extra' => string ''
  2 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '02:00'
      'end' => string '03:00'
      'desc' => string 'Event description 10-3'
      'price' => string ''
      'extra' => string ''
  3 => 
    array (size=9)
      'cat' => string ''
      'dur' => string '20'
      'type' => string 'Type description 20'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 20-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  4 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '01:00'
      'end' => string '02:00'
      'desc' => string 'Event description 20-2'
      'price' => string ''
      'extra' => string ''
  5 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string 'Type description 21'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 21-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  6 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '01:00'
      'end' => string '02:00'
      'desc' => string 'Event description 21-2'
      'price' => string ''
      'extra' => string ''
  7 => 
    array (size=9)
      'cat' => string 'SECOND CAT'
      'dur' => string '10'
      'type' => string 'Type description 100'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 100-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  8 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '01:00'
      'end' => string '02:00'
      'desc' => string 'Event description 100-2'
      'price' => string ''
      'extra' => string ''
  9 => 
    array (size=9)
      'cat' => string ''
      'dur' => string '20'
      'type' => string 'Type description 200'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 200-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  10 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string ''
      'start' => string '01:00'
      'end' => string '02:00'
      'desc' => string 'Event description 200-2'
      'price' => string ''
      'extra' => string ''
  11 => 
    array (size=9)
      'cat' => string ''
      'dur' => string ''
      'type' => string 'Type description 210'
      'start' => string '00:00'
      'end' => string '01:00'
      'desc' => string 'Event description 210-1'
      'price' => string 'xxx'
      'extra' => string 'yyy'
  etc...

我需要首先按

cat
对该数组进行分组,然后按
dur
,然后
type
进行分组。这个
type
数组将有两个字符串 -
price
extra
以及另一个包含所有事件数据的数组 -
start
end
desc
。最终的数组轮廓(基于上面的示例)应如下所示:

'FIRST CAT'                                           // 'cat'
    '10'                                              // 'dur'
      'Type description 10', 'xxx', 'yyy'             // 'type', 'price', 'extra'
          (array of events)
          '00:00', '01:00', 'Event description 10-1'  // 'start', 'end', 'desc'
          '01:00', '02:00', 'Event description 10-2'  // 'start', 'end', 'desc'
          '02:00', '03:00', 'Event description 10-3'  // 'start', 'end', 'desc'
    '20'
      'Type description 20', 'xxx', 'yyy'
          (array of events)
          '00:00', '01:00', 'Event description 20-1'
          '01:00', '02:00', 'Event description 20-2'
      'Type description 21', 'xxx', 'yyy'
          (array of events)
          '00:00', '01:00', 'Event description 21-1'
          '01:00', '02:00', 'Event description 21-2'
'SECOND CAT'
    '10'
      'Type description 100', 'xxx', 'yyy'
          (array of events)
          '00:00', '01:00', 'Event description 100-1'
          '01:00', '02:00', 'Event description 100-2'
    '20'
      'Type description 200', 'xxx', 'yyy'
          (array of events)
          '00:00', '01:00', 'Event description 200-1'
          '01:00', '02:00', 'Event description 200-2'
      'Type description 210', 'xxx', 'yyy'
          (array of events)
          '00:00', '01:00', 'Event description 210-1'
          '01:00', '02:00', 'Event description 210-2'

将现有数组转换为我最终需要的数组的最简单方法是什么?

php arrays multidimensional-array grouping hierarchical
2个回答
1
投票

以下内容满足您的需求:

$out = array();
foreach ($arr as $key => $value){
    $cat = $value['cat'];
    $dur = $value['dur'];
    $type = $value['type'];
    $out[$cat][$dur][$type][] = $value['start'].', '.$value['end'].', '.$value['desc'];
}
print_r($out);

编辑:

我修改了我的代码,因为请求在类别为空时使用最后一个类别。所以,这是我修改后的代码:

    $out = array();
    $last_cat = '';
    foreach ($arr as $key => $value){
        $cat = $value['cat'];
        $dur = $value['dur'];
        $type = $value['type'];
        if (empty($cat)){
            $cat = $last_cat;
        } else {
            $last_cat = $cat;
        }
        $out[$cat][$dur][$type][] = $value['start'].', '.$value['end'].', '.$value['desc'];
    }
    print_r($out);

这段代码的输出如下:

[FIRST CAT] => Array(
        [10] => Array(
            [Type description 10] => Array(
                [0] => 00:00, 01:00, Event description 10-1
            )
        )
        [] => Array(
            [] => Array(
                    [0] => 01:00, 02:00, Event description 10-2
                    [1] => 02:00, 03:00, Event description 10-3
                    [2] => 01:00, 02:00, Event description 20-2
                    [3] => 01:00, 02:00, Event description 21-2
                )

            [Type description 21] => Array(
                    [0] => 00:00, 01:00, Event description 21-1
                )
        )
        [20] => Array(
            [Type description 20] => Array(
                    [0] => 00:00, 01:00, Event description 20-1
                )
        )
    )
[SECOND CAT] => Array(
        [10] => Array(
            [Type description 100] => Array(
                    [0] => 00:00, 01:00, Event description 100-1
                )
            )
        [] => Array(
            [] => Array(
                    [0] => 01:00, 02:00, Event description 100-2
                    [1] => 01:00, 02:00, Event description 200-2
                )
            [Type description 210] => Array(
                    [0] => 00:00, 01:00, Event description 210-1
                )
            )
        [20] => Array(
            [Type description 200] => Array(
                    [0] => 00:00, 01:00, Event description 200-1
                )
            )
    )

-1
投票

我认为最好使用 http://php.net/manual/en/function.ksort.php ksort 函数。

© www.soinside.com 2019 - 2024. All rights reserved.