Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 :(

share|improve this question
    
is this values are coming from table? if yes which DB u r using? You can do it in query itself. – Suresh Kamrushi Aug 6 '13 at 8:37
    
You say you want to sort by symbol, but your code doesn't mention symbol at all: return $sizes[$a['symbol']] - $sizes[$b['symbol']]; perhaps – Mark Baker Aug 6 '13 at 8:38
    
no it is from php array – jasne Aug 6 '13 at 8:38

1 Answer 1

up vote 1 down vote accepted

Your sort function should read

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

The arguments $a and $b are items from inside your array, which in this case are themselves arrays. You have to grab the appropriate element from inside them to sort with.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.