doujiangqu2823 2016-02-21 11:07
浏览 22
已采纳

有人可以解释内部的PHP数组排序代码如何工作?

I have some problems with PHP right now. I don't know how to create a code which sorts numbers in array from lowest to highest (and contrariwise). I'm only allowed to use loops and conditions without using PHP built-in functions, so I can understand how these functions work.

I found this code on Stack overflow sorting array value without using built in php like sort() etc:

<?php

$array=array('2','4','8','5','1','7','6','9','10','3');

echo "Unsorted array is: ";
echo "<br />";
print_r($array);


for($j = 0; $j < count($array); $j ++) {
    for($i = 0; $i < count($array)-1; $i ++){

        if($array[$i] > $array[$i+1]) {
            $temp = $array[$i+1];
            $array[$i+1]=$array[$i];
            $array[$i]=$temp;
        }       
    }
}

echo "Sorted Array is: ";
echo "<br />";
print_r($array);

?>

Can someone please explain step by step on each line how part of the code below works? I lose concentration when trying to understand this.

for($j = 0; $j < count($array); $j ++) {
        for($i = 0; $i < count($array)-1; $i ++){

            if($array[$i] > $array[$i+1]) {
                $temp = $array[$i+1];
                $array[$i+1]=$array[$i];
                $array[$i]=$temp;
            }       
        }
    } 
  • 写回答

3条回答 默认 最新

  • dongpa3109 2016-02-21 11:35
    关注

    Just relax. :) This is one of the easiest code.

    In your code:

    <?php
    
    $array=array('2','4','8','5','1','7','6','9','10','3');
    

    //array declared with members

    echo "Unsorted array is: ";
    echo "<br />";
    print_r($array);
    

    //will output the present condition of your array

    for($j = 0; $j < count($array); $j ++) {
    

    //this is the first for loop. here, count($array) will return 10 as you have 10 members inside your array. So simply it just for($j = 0; $j < 10; $j ++). This for will loop through 0-9.

    for($i = 0; $i < count($array)-1; $i ++){
    

    //this is the second for loop. here, count($array)-1 will return 9. So simply it just for($i = 0; $i < 9; $i ++). This for will loop through 0-8.

        if($array[$i] > $array[$i+1]) {
                $temp = $array[$i+1];
                $array[$i+1]=$array[$i];
                $array[$i]=$temp;
            }       
        }
    }
    

    //this is the main part of your code. It check if a array member is greater than it's next member. If so then it swap the position or the code continue as there is no "else" statement. Suppose value of i is 0. Then if($array[0] > $array[1])->if(2>4) answer is no, so the code continue. Suppose value of i is 2. Then if($array[2] > $array[3])->if(8>5) answer is yes, now just swapping position happen. $temp=5 then $array[3]=$array[2] then $array[2]=5.

    echo "Sorted Array is: ";
    echo "<br />";
    print_r($array);
    
    ?>
    

    //just print the sorted array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错