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
  • 19,067
  • 8
  • 59
  • 78
user231430
  • 17
  • 3

3 Answers3

4

I would use uasort

Myles
  • 18,830
  • 3
  • 24
  • 35
1

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

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

vgoff
  • 9,856
  • 3
  • 35
  • 54
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,595
  • 10
  • 39
  • 64