I have the following multidimensional array
Array ( [June 2015] => Array ( [LOW] => Array ( [0] => 160.50 ) [MEDIUM] => Array ( [0] => 0.00 ) [HIGH] => Array ( [0] => 60.80 ) ) [July 2015] => Array ( [MEDIUM] => Array ( [0] => 226.00 ) [HIGH] => Array ( [0] => 263.00 ) [LOW] => Array ( [0] => 121.96 ) ) )
I need to sort each of the inner arrays by their key so they are in the order LOW, MEDIUM, HIGH (the first is correct by chance).
I tried the following code which I took and adjusted from here:
function cmp($a, $b){ $a = preg_replace('@^(LOW|MEDIUM|HIGH) @', '', $a); $b = preg_replace('@^(LOW|MEDIUM|HIGH) @', '', $b); return strcasecmp($a, $b); } foreach($live_quotations as $exp_conversion_date => $Aconversion_likelihood){ foreach($Aconversion_likelihood as $conversion_likelihood => $quotation_values){ uksort($live_quotations[$exp_conversion_date], "cmp"); } }
but this orders them as HIGH, MEDIUM, LOW (ascending alphabetical). It does not matter if I change the order in the cmp function they are always sorted this way. I don’t think I’m understanding this uksort or cmp function correctly. Any help will be great!
Thanks
Advertisement
Answer
You could use the following comparison function:
function cmp($a, $b) { $order = Array( 'LOW' => 0, 'MEDIUM' => 1, 'HIGH' => 2 ); return $order[$a] - $order[$b]; }
Example of this code is here.