php 多维数组按键值分类

背景:现在有一个多维数组,该数组里面的部分一维数组key值是完全一样的,现将一样的数组提取出来,用于做列表展示。

封装方法:

 1 function arrClassify($arr){
 2     $data = array();
 3     $a = 0;
 4     while ($arr){
 5         $key = $arr[0];
 6         $data[$a]['query'] = '第'.$a.'类';
 7         $data[$a]['head'] = array_keys($key);
 8         $data[$a]['value'][] = $key;
 9         unset($arr[0]);
10         $i = 1;
11         while($i <= count($arr)){
12             $result=array_diff_key($arr[$i],$key);
13             if(!$result){
14                 $data[$a]['value'][] = $arr[$i];
15                 unset($arr[$i]);
16             }
17             $i++;
18         }
19         $arr = array_values($arr);
20         $a++;
21     }
22     return $data;
23 }
封装的方法

 示例:

 1 <?php
 2 $arr = array(
 3     array(
 4         'a' => 1,
 5         'b' => 1,
 6         'c' => 1,
 7         'd' => 1,
 8     ),
 9     array(
10         'e' => 1,
11         'd' => 1,
12         'f' => 1,
13         'g' => 1,
14     ),
15     array(
16         'a' => 2,
17         'b' => 2,
18         'c' => 2,
19         'd' => 2,
20     ),
21     array(
22         'e' => 2,
23         'd' => 2,
24         'f' => 2,
25         'g' => 2,
26     ),
27     array(
28         'e' => 2,
29         'd' => 2,
30         'f' => 2,
31         'g' => 2,
32         'h' => 2,
33     ),
34     array(
35         'e' => 2,
36         'd' => 3,
37         'f' => 9,
38         'g' => 2,
39         'h' => 3,
40     ),
41 );
42 $a = arrClassify($arr);
43 var_dump($a);
测试用例

运行结果:

测试结果
 1 array (size=3)
 2   0 => 
 3     array (size=3)
 4       'query' => string '第0类' (length=7)
 5       'head' => 
 6         array (size=4)
 7           0 => string 'a' (length=1)
 8           1 => string 'b' (length=1)
 9           2 => string 'c' (length=1)
10           3 => string 'd' (length=1)
11       'value' => 
12         array (size=2)
13           0 => 
14             array (size=4)
15               'a' => int 1
16               'b' => int 1
17               'c' => int 1
18               'd' => int 1
19           1 => 
20             array (size=4)
21               'a' => int 2
22               'b' => int 2
23               'c' => int 2
24               'd' => int 2
25   1 => 
26     array (size=3)
27       'query' => string '第1类' (length=7)
28       'head' => 
29         array (size=4)
30           0 => string 'e' (length=1)
31           1 => string 'd' (length=1)
32           2 => string 'f' (length=1)
33           3 => string 'g' (length=1)
34       'value' => 
35         array (size=2)
36           0 => 
37             array (size=4)
38               'e' => int 1
39               'd' => int 1
40               'f' => int 1
41               'g' => int 1
42           1 => 
43             array (size=4)
44               'e' => int 2
45               'd' => int 2
46               'f' => int 2
47               'g' => int 2
48   2 => 
49     array (size=3)
50       'query' => string '第2类' (length=7)
51       'head' => 
52         array (size=5)
53           0 => string 'e' (length=1)
54           1 => string 'd' (length=1)
55           2 => string 'f' (length=1)
56           3 => string 'g' (length=1)
57           4 => string 'h' (length=1)
58       'value' => 
59         array (size=2)
60           0 => 
61             array (size=5)
62               'e' => int 2
63               'd' => int 2
64               'f' => int 2
65               'g' => int 2
66               'h' => int 2
67           1 => 
68             array (size=5)
69               'e' => int 2
70               'd' => int 3
71               'f' => int 9
72               'g' => int 2
73               'h' => int 3

posted @ 2019-03-25 11:00  小跑跑泡  阅读(544)  评论(0编辑  收藏  举报