<?php
$data = [
[
'id' => 2,
'position' => 2,
],
[
'id' => 1,
'position' => 1,
],
[
'id' => 1,
'position' => 0,
],
[
'id' => 2,
'position' => 1,
],
[
'id' => 2,
'position' => 0,
],
[
'id' => 1,
'position' => 2,
],
];
usort(
$data,
function ($a, $b) {
if ($a['position'] == $b['position']) {
return 0;
}
return ($a['position'] < $b['position']) ? -1 : 1;
}
);
// usort(
// $data,
// function ($a, $b) {
// if ($a['id'] == $b['id']) {
// return 0;
// }
// return ($a['id'] < $b['id']) ? -1 : 1;
// }
// );
var_dump($data);
- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array(6) {
[0]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(0)
}
[1]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(0)
}
[2]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(1)
}
[3]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(1)
}
[4]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(2)
}
[5]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(2)
}
}
- Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
- array(6) {
[0]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(0)
}
[1]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(0)
}
[2]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(1)
}
[3]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(1)
}
[4]=>
array(2) {
["id"]=>
int(1)
["position"]=>
int(2)
}
[5]=>
array(2) {
["id"]=>
int(2)
["position"]=>
int(2)
}
}
preferences:
146.81 ms | 403 KiB | 198 Q