如何对这个多维数组进行排序

时间:2012-06-06 01:18:15

标签: php sorting

我正在尝试更改购物车软件的输出。我相信我已经找到了阵列的来源,我只需要对它进行排序。 sort()不起作用。我使用print_r来查看数组,它比我的小脑能处理的维度更多。如何按完整货币名称对其进行排序?

Array ( [0] => Array ( [id] => USD [text] => US Dollar ) [1] => Array ( [id] => EUR [text] => Euro ) [2] => Array ( [id] => GBP [text] => United Kingdom Pound ) [3] => Array ( [id] => CAD [text] => Canadian Dollar ) [4] => Array ( [id] => AUD [text] => Australian Dollar ) [5] => Array ( [id] => CHF [text] => Swiss Franc ) [6] => Array ( [id] => CZK [text] => Czech Koruna ) [7] => Array ( [id] => DKK [text] => Danish Krone ) [8] => Array ( [id] => HKD [text] => Hong Kong Dollar ) [9] => Array ( [id] => HUF [text] => Hungarian Forint ) [10] => Array ( [id] => JPY [text] => Japanese Yen ) [11] => Array ( [id] => NZD [text] => New Zealand Dollar ) [12] => Array ( [id] => NOK [text] => Norwegian Kroner ) [13] => Array ( [id] => PLN [text] => Polish Zloty ) [14] => Array ( [id] => SGD [text] => Singapore Dollar ) [15] => Array ( [id] => SEK [text] => Swedish Krona ) [16] => Array ( [id] => ILS [text] => Israeli New Shekel ) [17] => Array ( [id] => MXN [text] => Mexican Peso ) [18] => Array ( [id] => TWD [text] => Taiwan Dollar ) [19] => Array ( [id] => PHP [text] => Philippine Peso ) [20] => Array ( [id] => THB [text] => Thai Baht ) )

1 个答案:

答案 0 :(得分:2)

正如Interstellar_Coder所说,使用usort,但做得对!

usort($array,create_function('$a,$b','return strnatcasecmp($a["text"],$b["text"]);'));

或者,从PHP 5.3开始:

usort($array,function($a,$b) {return strnatcasecmp($a['text'],$b['text']);});