drqja5919276 2019-07-22 09:22
浏览 43
已采纳

too long

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 : 
";

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
        )

)
  • 写回答

1条回答 默认 最新

  • duan33360 2019-07-22 09:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?