1

I have array like

Array ( 
     [608665839] => Array ( [score] => 2 ) 
     [1756044141] => Array ( [score] => 5 ) 
     [523536777] => Array ( [score] => 2 ) 
)

and I want to sore this array by score. How can I do?

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
user231430
  • 17
  • 3

3 Answers3

4

I would use uasort

Myles
  • 20,860
  • 4
  • 28
  • 37
1

I think [uasort()]1 function is helpful for sorting this array()

If multiple array then use array_[multisort()]2 functions

vgoff
  • 10,980
  • 3
  • 38
  • 56
user1972007
  • 323
  • 3
  • 19
0

From PHP.net:

<?php
    function order_array_num ($array, $key, $order = "ASC")
    {
        $tmp = array();
        foreach($array as $akey => $array2)
        {
            $tmp[$akey] = $array2[$key];
        }

        if($order == "DESC")
        {arsort($tmp , SORT_NUMERIC );}
        else
        {asort($tmp , SORT_NUMERIC );}

        $tmp2 = array();       
        foreach($tmp as $key => $value)
        {
            $tmp2[$key] = $array[$key];
        }       

        return $tmp2;
    }
?>

$order = "ASC" will sort the array in an ascending order while $order = "DESC" will sort the array in a descending order.

Hope this helps.

Valentin Flachsel
  • 10,795
  • 10
  • 44
  • 67