2014-09-10 43 views
0

我需要排序多个数组中的一个值。不幸的是,我无法在数据库中对其进行排序。我只能得到这样的数组。PHP多个数组排序

我的数组的样子:

Array 
(
    [0] => Array 
     (
      [id] => 10913 
      [name] => NAME 1 
      [logo_img] => 28361bd6114a32daafcf150da5ee1d33.jpeg 
      [has_frame] => t 
      [weight] => 0 
     ) 

    [1] => Array 
     (
      [id] => 10902 
      [name] => Name 2 
      [logo_img] => d0642d5efeabe8b861f2f32fa17f35b3.jpeg 
      [has_frame] => t 
      [weight] => 4 
     ) 

    [2] => Array 
     (
      [id] => 8887 
      [name] => Some name 3 
      [logo_img] => 12e47533604438bf390d69218434b630.jpeg 
      [has_frame] => f 
      [weight] => 0 
     ) 

    [3] => Array 
     (
      [id] => 49 
      [name] => Some name 4 
      [logo_img] => 5971148b049c5bb0b6e402a1de1836ef.jpeg 
      [has_frame] => t 
      [weight] => 0 
     ) 

    [4] => Array 
     (
      [id] => 10871 
      [name] => name2 
      [logo_img] => 7dcc1a6058a81b4e45de5f2274025907.jpeg 
      [has_frame] => t 
      [weight] => 0 
     ) 

    [5] => Array 
     (
      [id] => 1880 
      [name] => name 4 
      [logo_img] => dc05764ea71425a4a653b81997e7d929.jpeg 
      [has_frame] => f 
      [weight] => 0 
     ) 

    [6] => Array 
     (
      [id] => 9678 
      [name] => name 33 
      [logo_img] => 8ded98244a6928d5179398fa9d3b59bc.jpeg 
      [has_frame] => t 
      [weight] => 5 
     ) 

    [7] => Array 
     (
      [id] => 2880 
      [name] => name r 
      [logo_img] => 09876329a5ac2a84e9f83923c75e780c.jpeg 
      [has_frame] => f 
      [weight] => 0 
     ) 

    [8] => Array 
     (
      [id] => 8265 
      [name] => name 5 
      [logo_img] => 487ed3c676666e380f813f5f1a9fb040.jpeg 
      [has_frame] => t 
      [weight] => 1 
     ) 

) 

我需要订购这个数组重量元素DESC。可能吗?

+1

是的,这是可能的。你试过什么了? – 2014-09-10 11:23:40

+0

请参阅http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php – 2014-09-10 11:24:26

+0

我尝试array_multisort(),但没有效果。 – mcgoo 2014-09-10 11:25:31

回答

1

我这是怎么做的:

usort($object, 'cmp'); 

function cmp($a, $b) 
{ 
    return strcmp($b['weigth'], $a['weigth']); // or -> weigth 
} 
1
//You can use `uasort` that has a comparison function that can be defined by user; 


//if the name of the whole array is $array 

//define a function 

function compare($obj1,$obj2){ 
    if($obj1['weight']==$obj2['weight']){ 
     return 0; 
    } 
    return $obj1['weight']<$obj2['weight']?1:-1; 

} 
//the comparison operator in the function `compare` in the second return determines the order of the array elements. 


//Now call the function with your array 

uasort($array,'compare');