Answers
<?php
// php -S 127.0.0.1:8080 -t /www
// http://127.0.0.1:8080/BubbleSort.php
header('Content-Type: text/html; charset=utf-8');
ob_start();
$array = array(0,1,2,3,4,5,6,7,8,9);
$size = count($array);
for ($i=0;$i<$size;$i++) {
print_r($array);
for ($j=0;$j<$size-1-$i;$j++) {
if ($array[$j] < $array[$j+1]) {
$temp = $array[$j];
$array[$j] = $array[$j+1];
$array[$j+1] = $temp;
}
}
echo '<br />'.str_repeat(' ', 1024*4);
ob_flush();
flush();
sleep(1);
}
echo 'Done.';
ob_end_flush();
每隔1秒显示1行:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 0 )
Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 1 [9] => 0 )
Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 4 [1] => 5 [2] => 6 [3] => 7 [4] => 8 [5] => 9 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 7 [1] => 8 [2] => 9 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 8 [1] => 9 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Array ( [0] => 9 [1] => 8 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0 )
Done.
打开输出控制缓冲,不断的ob_flush();flush();,就可以在一个PHP请求内不断地输出内容,这个请求你可以通过AJAX发起.
東風谷灬早苗
answered 9 years, 5 months ago