PHP:对关联数组进行排序

时间:2015-01-12 14:06:48

标签: php arrays sorting

我有一个这种关联数组:

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函数可以命令数组,但我找不到合适的函数!

6 个答案:

答案 0 :(得分:2)

如果$ array是你的数组

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

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

答案 1 :(得分: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);

答案 2 :(得分:1)

尝试这样,假设您的数组名称为$array

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

print_r($array);

答案 3 :(得分: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 ) ) 

答案 4 :(得分: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以降序排序。

答案 5 :(得分: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($ tickets,array(&#39; id&#39;)); //这将在id列上对$ ticket进行排序。

示例输出:

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/