neeko Posted August 28, 2020 Report Posted August 28, 2020 I have an array in which score is a key and it has value like 2.0 , 7.8 , 9.8 etc. I want to sort that array by score ascending. array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r($allcomps); print_r shows without sorting. Quote
dasari4kntr Posted August 28, 2020 Report Posted August 28, 2020 12 minutes ago, neeko said: I have an array in which score is a key and it has value like 2.0 , 7.8 , 9.8 etc. I want to sort that array by score ascending. array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r($allcomps); print_r shows without sorting. <?php $allcomps = array( array( 'score' => 2.0, ), array( 'score' => 7.8, ), array( 'score' => 9.8, ) ); array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r ($allcomps); ?> Quote
neeko Posted August 28, 2020 Author Report Posted August 28, 2020 <?php $allcomps = array( array( 'score' => 7.0, ), array( 'score' => 7.8, ), array( 'score' => 2.8, ) ); array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r ($allcomps); ?> Not working. Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) ) Quote
dasari4kntr Posted August 28, 2020 Report Posted August 28, 2020 10 minutes ago, neeko said: <?php $allcomps = array( array( 'score' => 7.0, ), array( 'score' => 7.8, ), array( 'score' => 2.8, ) ); array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r ($allcomps); ?> Not working. Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) ) never mind its not working for me also...after chaning the input Quote
dasari4kntr Posted August 28, 2020 Report Posted August 28, 2020 27 minutes ago, neeko said: <?php $allcomps = array( array( 'score' => 7.0, ), array( 'score' => 7.8, ), array( 'score' => 2.8, ) ); array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC); print_r ($allcomps); ?> Not working. Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) ) 21 minutes ago, dasari4kntr said: never mind its not working for me also...after chaning the input try this...it is working.. <?php $allcomps = array( array( 'score' => '7.8', ), array( 'score' => '2.0', ), array( 'score' => '9.8', ) ); $a = array_column($allcomps, "score"); usort($a,"my_sort"); print_r ($a); function my_sort($a,$b) { if ($a==$b) return 0; return ($a<$b)?-1:1; } ?> Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.