基于多个条件的PHP多维数组排序

时间:2015-10-09 16:35:13

标签: php arrays sorting

让我们假设我有一个如下所示的数组

    $item = Array(

             [0] => Array(
                     "name" => "item1"
                     "score" => 100,
                     "category" => "A1",
                     "vote"  => 80,
               )

          [1] => Array(
                 "name" => "item2",
                 "score" => 100,
                 "category" => "A1",
                 "vote"    => 70,
           )
           [2] => Array(
                 "name" => "item3",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 80,
           )
           [3] => Array(
                 "name" => "item4",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 60,
           )
           [4] => Array(
                 "name" => "item5",
                 "score" => 80,
                 "category" => "A3",
                 "vote"    => 80,
           )
    )

编辑输入:

    Array
(
    [0] => Array
        (
            [name] => item1
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [1] => Array
        (
            [name] => item2
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [2] => Array
        (
            [name] => item5
            [score] => 80
            [category] => A3
            [vote] => 80
        )

    [3] => Array
        (
            [name] => item3
            [score] => 80
            [category] => A2
            [vote] => 80
        )

    [4] => Array
        (
            [name] => item4
            [score] => 80
            [category] => A2
            [vote] => 60
        )

)

排序的优先级为scorecategoryvote。 预期结果是:

$item = Array(

             [0] => Array(
                     "name" => "item1"
                     "score" => 100,
                     "category" => "A1",
                     "vote"    => 80,
               )
            [1] => Array(
                 "name" => "item5",
                 "score" => 80,
                 "category" => "A3",
                 "vote"    => 80,
            )
            [2] => Array(
                 "name" => "item3",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 80,
           )

          [3] => Array(
                 "name" => "item2",
                 "score" => 100,
                 "category" => "A1",
                 "vote"    => 70,
           )

           [4] => Array(
                 "name" => "item4",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 60,
           )

    )

编辑预期结果:

   Array
    (
    [0] => Array
        (
            [name] => item1
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [1] => Array
        (
            [name] => item5
            [score] => 80
            [category] => A3
            [vote] => 80
        )

    [2] => Array
        (
            [name] => item3
            [score] => 80
            [category] => A2
            [vote] => 80
        )
   [3] => Array
        (
            [name] => item2
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [4] => Array
        (
            [name] => item4
            [score] => 80
            [category] => A2
            [vote] => 60
        )

)

我希望根据score然后vote对顶部的不同类别进行排序,但如果它们是具有相同类别的两个项目,则分数较低的项目将具有较低的优先级

根据洞察力,我首先根据得分,类别和投票对数组进行排序

foreach($list as $k=>$v) {
        $sorted['score'][$k] = $v['score'];
        $sorted['category'][$k] = $v['category'];
        $sorted['vote'][$k] = $v['vote'];
    }

    array_multisort($sorted['score'], SORT_DESC, $sorted['category'], SORT_DESC,$sorted['vote'], SORT_DESC, $list);

它会给我:

    Array
(
    [0] => Array
        (
            [name] => item1
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [1] => Array
        (
            [name] => item2
            [score] => 100
            [category] => A1
            [vote] => 80
        )

    [2] => Array
        (
            [name] => item5
            [score] => 80
            [category] => A3
            [vote] => 80
        )



  [3] => Array
        (
            [name] => item3
            [score] => 80
            [category] => A2
            [vote] => 80
        )
    [4] => Array
        (
            [name] => item4
            [score] => 80
            [category] => A2
            [vote] => 60
        )

)

我需要再次对结果进行重新排序,以便在顶部显示不同的类别(请参阅编辑的预期结果)

编辑:看来我的例子和问题令人困惑。 我的想法是,我想根据分数对项目进行排序,然后进行投票,但应考虑该类别。除了得分和投票之外,我希望有时间在不同的类别上,然后得分和投票。

2 个答案:

答案 0 :(得分:1)

使用array_multisort()

<?php
$mylist = Array(

             0 => Array(
                     "name" => "item1",
                     "score" => 100,
                     "category" => "A1",
                     "vote"  => 80
               ),

          1 => Array(
                 "name" => "item2",
                 "score" => 100,
                 "category" => "A1",
                 "vote"    => 70
           ),
           2 => Array(
                 "name" => "item3",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 80
           ),
           3 => Array(
                 "name" => "item4",
                 "score" => 80,
                 "category" => "A2",
                 "vote"    => 60
           ),
           4 => Array(
                 "name" => "item5",
                 "score" => 80,
                 "category" => "A3",
                 "vote"    => 80
           )
    );

$sort = array();
foreach($mylist as $k=>$v) {
    $sort['score'][$k] = $v['score'];
    $sort['category'][$k] = $v['category'];
    $sort['vote'][$k] = $v['vote'];
}
# sort by event_type desc and then title asc
array_multisort($sort['vote'], SORT_DESC,$sort['score'], SORT_DESC, $sort['category'], SORT_DESC,$mylist);
    echo '<pre>';
    print_r($mylist);
    echo '</pre>';
?>

答案 1 :(得分:0)

注意:这不是答案,这只是一个建议。假设是一个评论,但有代码,而且很长。

首先,我刚刚将您的输入数组修改为此模型:

enter image description here

使用此代码:

$compiled = array();

for ($i = 0; $i < count($input_arr); $i++)
{
    $cat = $input_arr[$i]['category'];

    $index = count($compiled[$cat]);

    $compiled[$cat][$index]['name'] = $input_arr[$i]['name'];
    $compiled[$cat][$index]['score'] = $input_arr[$i]['score'];
    $compiled[$cat][$index]['vote'] = $input_arr[$i]['vote'];
    $compiled[$cat][$index]['overall'] = $input_arr[$i]['score'] + $input_arr[$i]['vote'];
}

我刚安排category。而且我还添加overall它只是得分和投票的总和。 :)

所有排序都来自:

foreach ($compiled as $k => $v)
{
    $json['sorted_by_vote'][$k] = sort_sub_value($compiled[$k], 'vote', false);

    $json['sorted_by_score'][$k] = sort_sub_value($compiled[$k], 'score', false);

    for ($i = 0;$i < count($compiled[$k]); $i++)
    {
        $json['sorted_by_overall'][] = $compiled[$k][$i];
    }
}
$json['sorted_by_overall'] = sort_sub_value($json['sorted_by_overall'], 'overall', false);

$json['input'] = $input_arr;

$json['revamp'] = $compiled;

echo json_encode($json);

function sort_sub_value($array, $subkey, $is_asc = true)
{
    foreach($array as $k=>$v) 
    {
        $b[$k] = strtolower($v[$subkey]);
    }

    if ($is_asc)
        asort($b); // asort -> asc | arsort ->desc
    else
        arsort($b);

    foreach($b as $key=>$val) 
    {
        $c[] = $array[$key];
    }
    return $c;
}

并将产生这个:

enter image description here

enter image description here

enter image description here

最后这里是示例输入,我只是弄乱votes进行测试。

enter image description here

希望这对其他原因有所帮助。干杯兄弟! :)