给定以下 PHP 数组:
Array
(
[0] => Array
(
[name] => Restaurant 123
[city] => Arlington
[zip] => 22201
)
[1] => Array
(
[name] => Bar Foo
[city] => Ballston
[zip] => 22201
)
[2] => Array
(
[name] => Restaurant XYZ
[city] => Ballston
[zip] => 22201
)
[3] => Array
(
[name] => Restaurant 321
[city] => Washington DC
[zip] => 22201
)
)
我怎样才能生成一个根据城市(按字母顺序)排序的列表,以便它输出如下内容:
阿灵顿
123号餐厅
鲍尔斯顿
酒吧富
XYZ 餐厅
华盛顿特区
321餐厅
E.G.,首先按城市名称字母顺序排序,然后按地点名称排序,同样按字母顺序排序。另请注意,餐厅名称和城市均未在给定数组中按字母顺序排序。
最佳答案
编写一个可以传递给usort的回调函数, 例如
function compare_venues($a, $b)
{
return strcmp($a['name'], $b['name']);
}
关于php - 根据值排序和显示数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4641723/