array_multisort不能正常使用PHP

时间:2015-04-25 18:29:31

标签: php arrays sorting

我需要你的帮助。我希望按优先级时间将数组排序为升序。

Heres是数组

Array
(
    [process] => Array
        (
            [0] => Array
                (
                    [name] => p1
                    [burst_time] => 2
                    [priority_time] => 3
                )

            [1] => Array
                (
                    [name] => p2
                    [burst_time] => 2
                    [priority_time] => 4
                )

            [2] => Array
                (
                    [name] => p3
                    [burst_time] => 2
                    [priority_time] => 1
                )

        )

)

我试过这段代码,但对我不起作用。谢谢你的高级:)

foreach ($data as $key => $row) {
    $mid[$key]  = $row;
}
array_multisort($mid, SORT_ASC, $data);

2 个答案:

答案 0 :(得分:1)

您使用的是array_multisort,但无需进行多维排序。一个简单的usort就足够了:

$data = array(
    "process" => array(
        array(
            "name" => p1,
            "burst_time" => 2,
            "priority_time" => 3
        ), array(
            "name" => p2,
            "burst_time" => 2,
            "priority_time" => 4
        ), array(
            "name" => p3,
            "burst_time" => 2,
            "priority_time" => 1
        )
    )
);

usort($data["process"], "sort_by_priority_time");

function sort_by_priority_time($a, $b) {
    return $a["priority_time"] - $b["priority_time"];
}

答案 1 :(得分:0)

我得到了它的工作。谢谢你们的帮助。

我把它改成了。最后它有效。

foreach ($data['process'] as $key => $row) {
    $mid[$key]  = $row;
}
array_multisort($mid, SORT_ASC, $data['process'] );