回答編集履歴
2
無名関数追加
test
CHANGED
@@ -151,3 +151,73 @@
|
|
151
151
|
$x = new Some();
|
152
152
|
|
153
153
|
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
無名関数にすれば煩わしくないか、と思ったりした。
|
158
|
+
|
159
|
+
(ソースはアレになりますが)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
- 無名関数版
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
<?php
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
$data = [
|
174
|
+
|
175
|
+
'a1' => [
|
176
|
+
|
177
|
+
'name' => 'zulu',
|
178
|
+
|
179
|
+
'age' => 24,
|
180
|
+
|
181
|
+
'hobby' => 'train',
|
182
|
+
|
183
|
+
],
|
184
|
+
|
185
|
+
'a2' => [
|
186
|
+
|
187
|
+
'name' => 'amemiya',
|
188
|
+
|
189
|
+
'age' => 28,
|
190
|
+
|
191
|
+
'hobby' => 'gourmet',
|
192
|
+
|
193
|
+
],
|
194
|
+
|
195
|
+
'a3' => [
|
196
|
+
|
197
|
+
'name' => 'sato',
|
198
|
+
|
199
|
+
'age' => 26,
|
200
|
+
|
201
|
+
'hobby' => 'radio',
|
202
|
+
|
203
|
+
],
|
204
|
+
|
205
|
+
];
|
206
|
+
|
207
|
+
usort($data, function($a, $b) {
|
208
|
+
|
209
|
+
if ($a['name'] === $b['name']) {
|
210
|
+
|
211
|
+
return $a['hobby'] > $b['hobby'];
|
212
|
+
|
213
|
+
} else {
|
214
|
+
|
215
|
+
return $a['name'] > $b['name'];
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
});
|
220
|
+
|
221
|
+
var_dump($data);
|
222
|
+
|
223
|
+
```
|
1
クラス対応版を追加
test
CHANGED
@@ -65,3 +65,89 @@
|
|
65
65
|
}
|
66
66
|
|
67
67
|
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
- クラス対応版
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
<?php
|
78
|
+
|
79
|
+
class Some {
|
80
|
+
|
81
|
+
function __construct() {
|
82
|
+
|
83
|
+
$this->solve();
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
function solve() {
|
90
|
+
|
91
|
+
$data = [
|
92
|
+
|
93
|
+
'a1' => [
|
94
|
+
|
95
|
+
'name' => 'sato',
|
96
|
+
|
97
|
+
'age' => 24,
|
98
|
+
|
99
|
+
'hobby' => 'train',
|
100
|
+
|
101
|
+
],
|
102
|
+
|
103
|
+
'a2' => [
|
104
|
+
|
105
|
+
'name' => 'amemiya',
|
106
|
+
|
107
|
+
'age' => 28,
|
108
|
+
|
109
|
+
'hobby' => 'gourmet',
|
110
|
+
|
111
|
+
],
|
112
|
+
|
113
|
+
'a3' => [
|
114
|
+
|
115
|
+
'name' => 'sato',
|
116
|
+
|
117
|
+
'age' => 26,
|
118
|
+
|
119
|
+
'hobby' => 'radio',
|
120
|
+
|
121
|
+
],
|
122
|
+
|
123
|
+
];
|
124
|
+
|
125
|
+
usort($data, 'self::cond');
|
126
|
+
|
127
|
+
var_dump($data);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
static function cond($a, $b) {
|
134
|
+
|
135
|
+
if ($a['name'] === $b['name']) {
|
136
|
+
|
137
|
+
return $a['hobby'] > $b['hobby'];
|
138
|
+
|
139
|
+
} else {
|
140
|
+
|
141
|
+
return $a['name'] > $b['name'];
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
$x = new Some();
|
152
|
+
|
153
|
+
```
|