2015-01-12 266 views
0

我有这样的关联数组:PHP:排序关联数组

array (size=3) 
    0 => 
    array (size=2) 
     'percent' => float 70.12 
     'txt' => string 'test' (length=4) 
    1 => 
    array (size=2) 
     'percent' => float 64.97 
     'txt' => string 'test' (length=4) 
    2 => 
    array (size=2) 
     'percent' => float 89.78 
     'txt' => string 'test' (length=4) 

,我需要通过percent字段排序。

例子:

array (size=3) 
    0 => 
    array (size=2) 
     'percent' => float 89.78 
     'txt' => string 'test' (length=4) 
    1 => 
    array (size=2) 
     'percent' => float 70.12 
     'txt' => string 'test' (length=4) 
    2 => 
    array (size=2) 
     'percent' => float 64.97 
     'txt' => string 'test' (length=4) 

我看到有几个PHP函数命令阵列,但我不能找到合适的一个!

+0

你想要的功能是[usort()](http://www.php.net/manual/en/function .usort.php)和看一看示例#2 –

+0

的适当的一个是'usort' –

+0

它是['usort()'](http://php.net/manual/en/function.usort.php),该允许您编写您需要的比较标准。 – axiac

回答

2

可以使用array_multisort,这样你就可以通过另一种排序领域也是。

foreach ($data as $key => $row) { 
    $percent[$key] = $row['percent']; 
    $txt[$key] = $row['txt']; 
} 
array_multisort($percent, SORT_DESC, $txt, SORT_ASC, $data); 
+0

感谢,这是更完整,但它足以让我通过单场 – Mariano

+0

'在array_multisort()排序'是不透明的,需要做一些准备,如这个答案所示,但它是一个非常强大的野兽。我认为它比大型数组的'usort()'快。 – axiac

1

尝试这样的,假设你的数组名是$array

usort($array, function ($a, $b) { 
    return $a['percent'] < $b['percent']; 
}); 

print_r($array); 
+0

这个作品,非常感谢! :) – Mariano

2

如果$数组是你的阵列

usort($array, function($a, $b) { 
    return $b['percent'] - $a['percent']; 
}); 

print_r($array); //to check it worked 
+0

谢谢,这工作很好! :) – Mariano

+0

尼斯招减去百分比值来获得比较函数的返回值 – axiac

+0

你不客气:) –

0
$array=array(array ('percent' => 70.12,'txt' =>'test'),array ('percent' => 64.97,'txt' =>'test'),array ('percent' => 89.78,'txt' =>'test')); 

array_multisort($array, SORT_DESC); 
print_r($array); 
//output Array ([0] => Array ([percent] => 89.78 [txt] => test) [1] => Array ([percent] => 70.12 [txt] => test) [2] => Array ([percent] => 64.97 [txt] => test)) 
0

试试这个(假设$list是你的阵列):

usort($list, 
    function(array $a, array $b) { 
     if ($a['percent'] < $b['percent']) { 
      return -1; 
     } elseif ($a['percent'] == $b['percent']) { 
      return 0; 
     } else { 
      return +1; 
     } 
    } 
) 

或交换-1+1降序排序。

0
/** 
* Sort a 2 dimensional array based on 1 or more indexes. 
* 
* msort() can be used to sort a rowset like array on one or more 
* 'headers' (keys in the 2th array). 
* 
* @param array  $array  The array to sort. 
* @param string|array $key  The index(es) to sort the array on. 
* @param int   $sort_flags The optional parameter to modify the sorting 
*         behavior. This parameter does not work when 
*         supplying an array in the $key parameter. 
* 
* @return array The sorted array. 
*/ 
function msort($array, $key, $sort_flags = SORT_REGULAR) { 
    if (is_array($array) && count($array) > 0) { 
     if (!empty($key)) { 
      $mapping = array(); 
      foreach ($array as $k => $v) { 
       $sort_key = ''; 
       if (!is_array($key)) { 
        $sort_key = $v[$key]; 
       } else { 
        // @TODO This should be fixed, now it will be sorted as string 
        foreach ($key as $key_key) { 
         $sort_key .= $v[$key_key]; 
        } 
        $sort_flags = SORT_STRING; 
       } 
       $mapping[$k] = $sort_key; 
      } 
      asort($mapping, $sort_flags); 
      $sorted = array(); 
      foreach ($mapping as $k => $v) { 
       $sorted[] = $array[$k]; 
      } 
      return $sorted; 
     } 
    } 
    return $array; 
} 

样品输入:

$tickets = array(
    array(
     'id' => 13, 
     'owner' => 'jachim', 
     'time' => '2009-09-25 10:39:42.011612', 
     'project' => 'jachim.be', 
     'title' => 'Some random ticket' 
    ), 
    array(
     'id' => 31, 
     'owner' => 'jachim', 
     'time' => '2009-09-24 14:38:47.945020', 
     'project' => 'joggink.be', 
     'title' => 'Some other random ticket' 
    ), 
    array(
     'id' => 22, 
     'owner' => 'root', 
     'time' => '2009-09-24 10:58:02.904198', 
     'project' => 'joggink.be', 
     'title' => 'A specific ticket' 
    ) 
); 

函数调用: msort($门票,阵列( 'ID')); //这将在id列上排序$票据。

样本输出:

Array 
(
    [0] => Array 
     (
      [id] => 13 
      [owner] => jachim 
      [time] => 2009-09-25 10:39:42.011612 
      [project] => jachim.be 
      [title] => Some random ticket 
     ) 

    [1] => Array 
     (
      [id] => 22 
      [owner] => root 
      [time] => 2009-09-24 10:58:02.904198 
      [project] => joggink.be 
      [title] => A specific ticket 
     ) 

    [2] => Array 
     (
      [id] => 31 
      [owner] => jachim 
      [time] => 2009-09-24 14:38:47.945020 
      [project] => joggink.be 
      [title] => Some other random ticket 
     ) 

) 

参考:

http://blog.jachim.be/2009/09/php-msort-multidimensional-array-sort/comment-page-1/