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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么