0

I have an array :

array(1) { ["myarr"]=> array(5) {
[0]=> array(2) { ["symbol"]=> string(6) "EUR" ["desc"]=> string(6) "da" }
[1]=> array(2) { ["symbol"]=> string(6) "USD" ["desc"]=> string(6) "adad" }
[2]=> array(2) { ["symbol"]=> string(6) "CHF" ["desc"]=> string(6) "das23" }
[3]=> array(2) { ["symbol"]=> string(6) "GBP" ["desc"]=> string(6) "dd12" }
[4]=> array(2) { ["symbol"]=> string(6) "NOR" ["desc"]=> string(6) "233" }
  }
} 

Now i need sort array by symbol in order: NOR, USD, EUR, CHF , GBP

So i wrote callback function

 uasort($myarr , 'sort_myarr')



 function sort_myarr($a, $b) {

 static $sizes = array( 'NOR' => 0, 'USD' => 1, 'EUR' => 2, 'CHF' => 3, 'GBP' => 4);


 return $sizes[$a] - $sizes[$b];
}     

But doesn't sort :(

4

1 回答 1

1

您的排序功能应为

return $sizes[$a['symbol']] - $sizes[$b['symbol']];

参数$a$b是数组内部的项目,在这种情况下,它们本身就是数组。您必须从它们内部获取适当的元素进行排序。

于 2013-08-06T08:38:08.117 回答