how to sort two dimentional array in descending array in php? -


this question has answer here:

i have following array , want sort array in descending order on base of "count" index value in php. have used following code not working me. please give me hint sort array in descending order.

array:-

array ( [0] => array ( [text] => text [count] => 0 )          [1] => array ( [text] => second text [count] => 2 )          [2] => array ( [text] => third text [count] => 1 )       ) 

i have tried following code.

function sort_count($a, $b) {     return $a['count'] - $b['count']; } $sorted_array = usort($array, 'sort_count'); 

try this:

note: checking equality acts added advantage.

function sort_count($a, $b) {    if ($a['count'] === $b['count']) {       return 0;    } else {        return ($a['count'] > $b['count'] ? 1:-1);    } } $sorted_array = usort($array, 'sort_count');  echo "<pre>";  print_r($array);  echo "</pre>"; 

hope helps.


Comments