【问题标题】:Sorting array with multiple values compared比较多个值的排序数组
【发布时间】:2017-10-17 10:40:13
【问题描述】:

我有以下数组

$postarray[] = array( 'total'   => '6.5', 'points'  => '300' );
$postarray[] = array( 'total'   => '11.4', 'points' => '320' );
$postarray[] = array( 'total'   => '6.5', 'points'  => '340' );
$postarray[] = array( 'total'   => '2.6', 'points'  => '300' );
$postarray[] = array( 'total'   => '12.8', 'points' => '320' );

我想将它排序成以下内容。请注意,如果 points 相等,则它会比较 total 并按升序对其进行排序。

$postarray[] = array( 'total'   => '6.5', 'points'  => '340' );
$postarray[] = array( 'total'   => '12.8', 'points' => '320' );
$postarray[] = array( 'total'   => '11.4', 'points' => '320' );
$postarray[] = array( 'total'   => '6.5', 'points'  => '300' );
$postarray[] = array( 'total'   => '2.6', 'points'  => '300' );

到目前为止,我已经使用usort 根据points 进行排序,但我也不确定如何对total 字段进行排序。

    function sortByOrder($a, $b) {
        if ($b['points'] > $a['points']) {
            return $b['points'] - $a['points'];
        } elseif ($b['points'] == $a['points']) {
            return 0;
        }
    }
    usort($postarray, 'sortByOrder');

【问题讨论】:

标签: php arrays sorting


【解决方案1】:

array_multisort()array_column() 自 PHP 5.5.0 起:

array_multisort(array_column($postarray, 'points'), SORT_DESC,
                array_column($postarray, 'total'),  SORT_DESC,
                $postarray);

【讨论】:

    【解决方案2】:

    您可以使用 php.net 提供到 sort 示例中的示例:

    <?php
    
    function array_sort($array, $on, $order=SORT_ASC)
    {
        $new_array = array();
        $sortable_array = array();
    
        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }
    
            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                break;
                case SORT_DESC:
                    arsort($sortable_array);
                break;
            }
    
            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }
    
        return $new_array;
    }
    
    $postarray[] = array( 'total'   => '6.5', 'points'  => '300' );
    $postarray[] = array( 'total'   => '11.4', 'points' => '320' );
    $postarray[] = array( 'total'   => '6.5', 'points'  => '340' );
    $postarray[] = array( 'total'   => '2.6', 'points'  => '300' );
    $postarray[] = array( 'total'   => '12.8', 'points' => '320' );
    
     print_r(array_sort($postarray, 'points', SORT_DESC));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2010-11-17
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多