简体   繁体   English

array_multisort无法使用PHP

[英]array_multisort not working PHP

Please i need your help. 拜托我需要你的帮忙。 I want to sort the array into an ascending order by priority time. 我想按优先级将数组排序为升序。

Heres is the array 这是数组

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
                )

        )

)

I tried this code but doesn't work for me. 我尝试了这段代码,但对我不起作用。 Thank you in advanced :) 谢谢你高级:)

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

You're using array_multisort but you don't need to sort in multiple dimensions. 您正在使用array_multisort但无需按多个维度进行排序。 A simple usort is enough: 一个简单的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"];
}

I got it working. 我知道了 Thank you guys for your help. 谢谢你们的帮助。

I changed it to. 我将其更改为。 And finally it works. 终于可以了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM