我在 Stack Overflow 上浏览了所有关于在 PHP 中对多维数组进行排序的答案,但没有一个直接回答我的问题。
从各种答案中,我了解到我应该使用 phpusort
函数或 phparray_multisort
函数,但我不确定如何将它们应用于我非常具体的数组结构:
这是我的变量$array
:
Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => abcORDERBYTHIS
)
)
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => ghiORDERBYTHIS
)
)
)
[2] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => defORDERBYTHIS
)
)
)
)
我希望能够field3
按. 使用 php函数访问这个元素很容易,如下所示:subarray
end
<?php
foreach($array as $array_single){
foreach(end($array_single['subarray']) as $sub_array){
echo $sub_array;
}
}
?>
从现在开始,我被困在如何按字母顺序专门对这个多维数组进行排序,以便我们得到以下结果:
$array[0]
- 保持领先,因为field3
价值是abcORDERBYTHIS
$array[2]
- 跳到中间,因为field3
值是defORDERBYTHIS
$array[1]
- 现在位于底部,因为field3
值为ghiORDERBYTHIS
提前致谢!