【发布时间】:2012-03-11 06:35:10
【问题描述】:
首先,是的,这是一个重复的问题,在这里被问了 100 次,我已经阅读了许多帖子,但是给出的示例对我来说并不是 100% 有效,我无法弄清楚我哪里错了。
我想通过“距离”键对这样的数组进行排序,并且我希望将排序“扩展”到整个数组中,以便对数组中的所有键重新排序。本质上,就像您对电子表格中的一列进行排序并将排序扩展到所有列一样。
这是示例数组:
$locations = array (
[phone] => Array (
[0] => 555-123-1234
[1] => 555-456-4321
[2] => 555-789-1122
[3] => 555-321-1111 )
[address] => Array (
[0] => 123 Main Street BigCity, NY 12345
[1] => 456 Maple Street Somewhere, UT 87654
[2] => 999 1st Ave MyTown, VA 23456
[3] => 321 2nd Ave MyTown, VA 23456 )
[distance] => Array (
[0] => 24.56
[1] => 13.05
[2] => 23.99
[3] => 1.75 )
[name] => Array (
[0] => First Location
[1] => Second Location
[2] => Third Location
[3] => Fourth Location )
)
我正在尝试按“距离”对数组进行排序,并且我尝试同时使用 array_multisort() 函数和 usort() 函数......但只有“multisort”通过排序给我提供了几乎可用的结果只有距离,而不是根据距离对整个数组进行排序。
我的功能:
array_multisort($locations['distance'], SORT_NUMERIC);
然后我运行一个“for”循环来遍历数组并按键吐出值。结果是距离已排序,但位置名称、地址等未重新排序。
我在这里错过了什么??
谢谢!!
【问题讨论】:
标签: php multidimensional-array sorting