回答編集履歴
1
niji
test
CHANGED
@@ -37,3 +37,73 @@
|
|
37
37
|
*/
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
# 二次配列の場合
|
44
|
+
|
45
|
+
二次配列でも考え方は同じ
|
46
|
+
|
47
|
+
```PHP
|
48
|
+
|
49
|
+
<?PHP
|
50
|
+
|
51
|
+
$a=[
|
52
|
+
|
53
|
+
['id' => '2' ,'category_id' => 'A',],
|
54
|
+
|
55
|
+
['id' => '30' ,'category_id' => 'A',],
|
56
|
+
|
57
|
+
['id' => '12' ,'category_id' => 'B',],
|
58
|
+
|
59
|
+
['id' => '8' ,'category_id' => 'B',],
|
60
|
+
|
61
|
+
['id' => '10' ,'category_id' => 'C',],
|
62
|
+
|
63
|
+
['id' => '329' ,'category_id' => 'D',],
|
64
|
+
|
65
|
+
['id' => '89' ,'category_id' => 'E',],
|
66
|
+
|
67
|
+
['id' => '79' ,'category_id' => 'E',],
|
68
|
+
|
69
|
+
['id' => '58' ,'category_id' => 'A',],
|
70
|
+
|
71
|
+
['id' => '219' ,'category_id' => 'B',],
|
72
|
+
|
73
|
+
['id' => '44' ,'category_id' => 'B',],
|
74
|
+
|
75
|
+
['id' => '3219' ,'category_id' => 'B',],
|
76
|
+
|
77
|
+
['id' => '231' ,'category_id' => 'B',],
|
78
|
+
|
79
|
+
['id' => '43210','category_id' => 'F',],
|
80
|
+
|
81
|
+
];
|
82
|
+
|
83
|
+
$b=[];
|
84
|
+
|
85
|
+
$c=[];
|
86
|
+
|
87
|
+
while(count($a)>0){
|
88
|
+
|
89
|
+
//shuffle($a);
|
90
|
+
|
91
|
+
$category=isset($c["category_id"])?$c["category_id"]:null;
|
92
|
+
|
93
|
+
$a=array_merge(array_filter($a,function($x) use($category){
|
94
|
+
|
95
|
+
return $x["category_id"]!=$category;
|
96
|
+
|
97
|
+
}),array_filter($a,function($x) use($category){
|
98
|
+
|
99
|
+
return $x["category_id"]==$category;
|
100
|
+
|
101
|
+
}));
|
102
|
+
|
103
|
+
$b[]=($c=array_splice($a,0,1)[0]);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
print json_encode($b);
|
108
|
+
|
109
|
+
```
|