我正在尝试按特定顺序对数组进行排序:
我的代码(当前数组):
Array
(
[25] => Array
(
[1st place] =>
)
[15] => Array
(
[2nd place] =>
)
[10] => Array
(
[3rd place] =>
)
[5] => Array
(
[4th place] =>
[5th place] =>
[6th place] =>
[7th place] =>
[8th place] =>
[9th place] =>
[10th place] =>
)
[1] => Array
(
[11th place] =>
[12th place] =>
[13th place] =>
[14th place] =>
[15th place] =>
[16th place] =>
[17th place] =>
[18th place] =>
[19th place] =>
[20th place] =>
[21st place] =>
[22nd place] =>
[23rd place] =>
[24th place] =>
[25th place] =>
)
)
Run Code Online (Sandbox Code Playgroud)
需要数组:
Array
(
[0] => Array
(
[1st place] => 25
)
[1] => Array
(
[2nd place] => 15
)
[5] => Array
(
[3rd place] => 10
)
[3] => Array
(
[4th place] => 5
[5th place] => 5
[6th place] => 5
[7th place] => 5
[8th place] => 5
[9th place] => 5
[10th place] => 5
)
[4] => Array
(
[11th place] => 1
[12th place] => 1
[13th place] => 1
[14th place] => 1
[15th place] => 1
[16th place] => 1
[17th place] => 1
[18th place] => 1
[19th place] => 1
[20th place] => 1
[21st place] => 1
[22nd place] => 1
[23rd place] => 1
[24th place] => 1
[25th place] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
这个想法就像使用数组位置(?)作为键的值并按此顺序设置数组:
我正在尝试使用该代码(php)
foreach ($newOptions as $ord) {
$place = $ord[$idx];
$value = $ord[$idx][0];
$newOrderarr[$idx][$value][$place] = $name2;
$idx++;
}
Run Code Online (Sandbox Code Playgroud)
但是,并没有正常工作$ newoptions是我正在使用的数组...
没有人选择明显的排序方法?
uasort($data, function($a, $b) {
reset($a);
reset($b);
$aVal = (int) key($a);
$bVal = (int) key($b);
if ($aVal < $bVal) {
return -1;
} elseif ($bVal < $aVal) {
return 1;
} else {
return 0;
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,这会重置子数组中的内部指针,但这不太可能是一个问题,无论如何它们可能都是副本.此外,如果你有一个空的子数组,它将无法工作.在那种情况下,我只是先调用array_filter它来删除空值.