简体   繁体   中英

How to sort multi-dimensional array with bubble sort in PHP?

My bubble sort is perfectly working for single array . How can i implement this for multi-dimensional array ? I want to sort this multi dimensional array value ['position'] .

CODE:

$itemId = $parentTicket[0]['id'];

function bubbleSort(&$arr)
{
    $n = sizeof($arr);

    for($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $n - $i - 1; $j++)
        {
            if ($arr[$j] > $arr[$j+1])
            {
                $t = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $t;
            }
        }
    }
}

$sql = "SELECT item.id, item.protocol, item.position, item.subject_item, item.type, item.responsible, item.INSDATE, item.body, pp.participant, p.subject, p.status FROM protocol p LEFT JOIN protocol_item item ON item.protocol = p.id LEFT JOIN protocol_participant pp ON pp.itemid = item.id WHERE item.protocol = $itemId GROUP BY item.id";
$arr = $global->db->getQuery($sql);
//$arr = array(64, 34, 25, 12, 22, 11, 90); // it's working

$len = sizeof($arr);
bubbleSort($arr);

echo "Sorted array : \n";

for ($i = 0; $i < $len; $i++) {
     $final_position = $arr[$i];
     echo $final_position . " ";

}

$pos_sql return:

Array
(
    [0] => Array
        (
            [id] => 15
            [protocol] => 12
            [position] => 2
            [subject_item] => 
            [type] => D
            [responsible] => Chonchol
            [INSDATE] => 2019-07-03 11:33:13
            [body] => With responsible
            [participant] => Chonchol,Mahmud,more
            [subject] => Check for multi-participant final
            [status] => 0
        )

    [1] => Array
        (
            [id] => 16
            [protocol] => 12
            [position] => 2
            [subject_item] => 
            [type] => D
            [responsible] => Mahmud
            [INSDATE] => 2019-07-03 11:39:01
            [body] => With Responsible
            [participant] => Donald,Trump,Brad,Pitt
            [subject] => Check for multi-participant final
            [status] => 0
        )

    [2] => Array
        (
            [id] => 17
            [protocol] => 12
            [position] => 6
            [subject_item] => 
            [type] => I
            [responsible] => Testing
            [INSDATE] => 2019-07-03 11:43:01
            [body] => Another body bcfb
            [participant] => 
            [subject] => Check for multi-participant final
            [status] => 0
        )

    [3] => Array
        (
            [id] => 21
            [protocol] => 12
            [position] => 1
            [subject_item] => This is form Update
            [type] => S
            [responsible] => three
            [INSDATE] => 2019-07-13 04:39:10
            [body] => ffffffffffff
            [participant] => 
            [subject] => Check for multi-participant final
            [status] => 0
        )

)

Using usort () you can do that. Working demo .

usort($arr, function ($a, $b) {
    return $a['position'] > $b['position'] ? 1 : -1;
});

print '<pre>';
print_r($arr);

With the help of bubble sort. In your code you should compare with two position but your comparison is with array indexes. A bit modification is need: change $arr[$j] > $arr[$j+1] to $arr[$j]['position'] > $arr[$j+1]['position'] .

$n = sizeof($arr);

for($i = 0; $i < $n; $i++) {
    for ($j = 0; $j < $n - $i - 1; $j++) {
        if ($arr[$j]['position'] > $arr[$j+1]['position']) {
            $t = $arr[$j];
            $arr[$j] = $arr[$j+1];
            $arr[$j+1] = $t;
        }
    }
}

Working demo .

Note: I don't know why do you need bubble sort instead of usort() . usort() is much simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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