Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按升序排序PHP数组_Php_Algorithm_Sorting - Fatal编程技术网

按升序排序PHP数组

按升序排序PHP数组,php,algorithm,sorting,Php,Algorithm,Sorting,尝试按升序对PHP数组排序,但松开了夹点 <?php $array = [4,5,63,2,1,66,43]; $count = count($array); $counter = 0; for ($i=0; $i < count($array); $i++) { if($counter > 100) break; if($array[$i] < $array[$i + 1]) { continue; } else

尝试按升序对PHP数组排序,但松开了夹点

<?php

$array = [4,5,63,2,1,66,43];
$count = count($array);
$counter = 0;
for ($i=0; $i < count($array); $i++) {
    if($counter > 100)
        break;
    if($array[$i] < $array[$i + 1]) {
        continue;
    } else {
        $now_i = $array[$i];
        unset($array[$i]);
        $array[] = $now_i;
        $array = array_values($array);
        $i = 0;
    }
    print_r($array);
    $counter++;
}
如果有人能帮我解决这个问题,我会很感激的,因为我花了4个小时思考出了什么问题


谢谢

您正在设置
$i=0但是循环会增加它!您应该将其设置为-1

<?php

$array = [4,5,63,2,1,66,43];
$count = count($array);
$counter = 0;
for ($i=0; $i < count($array); $i++) {
    if($counter > 100)
        break;
    // this ends the program when complete
    if (!isset($array[$i + 1])) exit;
    if($array[$i] < $array[$i + 1]) {
        continue;
    } else {
        $now_i = $array[$i];
        unset($array[$i]);
        $array[] = $now_i;
        $array = array_values($array);
        // this is the fix
        $i = -1;
    }
    print_r($array);
    $counter++;
}

您正在设置
$i=0但是循环会增加它!您应该将其设置为-1

<?php

$array = [4,5,63,2,1,66,43];
$count = count($array);
$counter = 0;
for ($i=0; $i < count($array); $i++) {
    if($counter > 100)
        break;
    // this ends the program when complete
    if (!isset($array[$i + 1])) exit;
    if($array[$i] < $array[$i + 1]) {
        continue;
    } else {
        $now_i = $array[$i];
        unset($array[$i]);
        $array[] = $now_i;
        $array = array_values($array);
        // this is the fix
        $i = -1;
    }
    print_r($array);
    $counter++;
}

请尝试下面的代码

$array = [4,5,63,2,1,66,43];
sort($array);
$len = count($array);

for($x=0;$x<$len;$x++)
{
    echo $array[$x];
    echo '<br>';
}
$array=[4,5,63,2,1,66,43];
排序($数组);
$len=计数($array);

对于($x=0;$x请尝试下面的代码

$array = [4,5,63,2,1,66,43];
sort($array);
$len = count($array);

for($x=0;$x<$len;$x++)
{
    echo $array[$x];
    echo '<br>';
}
$array=[4,5,63,2,1,66,43];
排序($数组);
$len=计数($array);
对于($x=0;$x
$array=[4,5,63,2,1,66,43];
排序($数组);
echo“.print_r($array,1)。”;
排列
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 43
[5] => 63
[6] => 66
)
$array=[4,5,63,2,1,66,43];
排序($数组);
echo“.print_r($array,1)。”;
排列
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 43
[5] => 63
[6] => 66
)

sort()
完成了吗?!是的,在做生产工作的时候,我当然会这么做。因为我缺乏算法,所以这只是为了提高我的技能。看:提问的黄金法则:“想象你在尝试回答问题”首先,当迭代在数组中的最后一个元素上时,$i+1不存在。因此,这种比较是错误的:
if($array[$i]<$array[$i+1])
sort()
完成了吗?!是的,在做生产工作时,我显然会这样做。因为我缺乏算法,所以这只是为了提高我的技能。请看:提问的黄金法则:“想象你正在尝试回答问题”首先,当迭代在数组中的最后一个元素上时,$i+1不存在。因此,这种比较是错误的:
if($array[$i]<$array[$i+1])
这如何解决
$array[$i+1]未定义的偏移量问题
?@devlnetranate LOL它没有。它解决了OP的排序问题。我想我也可以清理掉那个通知,但我怀疑它对未来的读者有什么价值:)@Devlennate解决了所有问题。它不仅修复了未定义的索引通知,而且还终止了程序,而不是到达计数器的末尾。我只是很难找到一个有错误的答案;)这如何解决
$array[$i+1]
的未定义偏移量问题?@Devlennate LOL它没有。它解决了OP的排序问题。我想我也可以把那个通知清理干净,但我怀疑它对未来的读者有什么价值:)@develenvanite好了。它不仅修复了未定义的索引通知,而且还终止了程序,而不是走到计数器的末尾。我只是很难找到有错误的答案;)