[英]Sort alphabetically an array taken dynamically
I have an array obtained from a json, and I want to sort its elements alphabetically. 我有一个从json获得的数组,我想按字母顺序对元素进行排序。
I have a function that returns me the result of the array: 我有一个函数,它返回数组的结果:
$cities = get_cities();
I tried both: 我都尝试过:
asort($cities['cities']);
foreach($cities['cities'] as $rc) { blah blah
and 和
foreach(asort($cities['cities']) as $rc) { blah blah
none did not do the sorting. 没有人没有进行排序。
any idea about where I am wrong? 对我错在哪里有任何想法吗? thanks!
谢谢!
Sorting by city names could look like 按城市名称排序可能看起来像
$c = $cities['cities'];
usort($c, function($cityA, $cityB) {
return strcmp($cityA['name'], $cityB['name']);
});
Without knowing what your $cities
array looks like, it is hard to tell what the problem is. 不知道您的
$cities
数组是什么样子,很难说出问题所在。
Have you tried using the just the regular PHP sort function? 您是否尝试过使用常规的PHP排序功能? ie
即
sort( $cities );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.