0

I am trying to change output on my shopping cart software. I believe I have found where the array is coming from and I just need to sort it. sort() didn't work. I used print_r to see the array, and it is more dimensions than my small brain can handle. How to sort this by the full currency name?

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
  • for readability, can you input your Array code on multiple lines in your question? Commented Sep 3, 2017 at 13:59

1 Answer 1

2

As Interstellar_Coder said, use usort, but do it right!

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

Or, from PHP 5.3 onwards:

usort($array,function($a,$b) {return strnatcasecmp($a['text'],$b['text']);});
Sign up to request clarification or add additional context in comments.

2 Comments

This is definitely a complete answer +1 for using an anonymous function.
I have no idea what "Yay for lambdas" means nor do I know what is meant by "anonymous function". Also, you say "As Interstellar_Coder said" but as far as I see, he didn't say anything until after you answered. And I assumed I would get a notification so I thought my question was just ignored. This site is confusing in its operation. Thanks for the answer though! I figured something else out but it was nowhere near as elegant. More readable, perhaps, but less elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.