05c05-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 37 行

PHP
37
字号
<?php// Implement the Toggle Sortfunction toggle_sort(&$a) {    // Prepare some starting values.    $i = 0;    $lastindex = count($a) - 1;    // Continue looping until we reach the last element.    while ($i < $lastindex) {        // Compare this value to the next one        if ($a[$i] <= $a[$i+1]) {            // It is less than or equal to the next one, therefore            //  this is the proper order, move on            $i++;        } else {            // It is greater than the next one, swap it            $tmp = $a[$i];            $a[$i] = $a[$i+1];            $a[$i+1] = $tmp;            // Step back one in our loop in case the value needs to rise            //  again.  Don't do this at the beginning of the array.:            if ($i) { $i--; }        }     }}// Prepare an array of values:$values = array(73, 3, 42, 6, 14, 23, 15, 9, 74, 1, 234, 45, 23, 76, 12, 3);// Sort them:toggle_sort($values);// Echo them out in order://  1 3 3 6 9 12 14 15 23 23 42 45 73 74 76 234foreach ($values as $v) { echo "{$v} "; } ?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?