php数组提前显示

时间:2013-07-19 08:29:05

标签: php arrays

这是我的数组

$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');

我希望显示如下:

第一行:first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelvelveth,thirteenth

第二行:second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelvelveth,thirteenth,first

第三行:third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelvelveth,thirteenth,first,second

第四行:fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelvelveth,thirteenth,first,second,third

等动态。

当我在主阵列上添加值或删除值时。输出也应该改变。

5 个答案:

答案 0 :(得分:0)

我这样做了:

$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');
print_r($list);
$new = array_shift($list);
$list[] = $new;
print_r($list);

编辑:(这显示它很好地通过阵列工作)

<?
$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');


for($i=0;$i<count($list);$i++) {
    echo "---- $i ---- <br /><br />";
    print_r($list)."<br />"; 
    $new = array_shift($list);
    $list[] = $new;
    echo "<br /><br />";
    print_r($list); 
    echo "<br />";
    echo "----------- <br /><br />";
}
?>

答案 1 :(得分:0)

//This wuill work for you
<?php
//list the array
$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');

//get the count
$count = count($list);

$val = '';
//loop the value
for($i=0; $i<$count; $i++)
{
    //skip one value
    $pop = array_shift($list);

    //appentd the value
    $val .= $pop.',';

    //display the value
    echo implode(',', $list).','.substr($val, 0, -1).'<br/>';

}

答案 2 :(得分:0)

许多方法,其中一个

<?php
//array
$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');
// array counter
$i=0;
// for loop max
$max = sizeof($list);

while(sizeof($list) != 0)
{   //for loop to echo data
    for($l=$i;$l<$max;$l++){
    echo $list[$l];
    // condition to not put comma after last number
    if($l != $max-1){echo ',';}
            }
// new line
echo '<br>';
// remove first element
unset($list[$i]);
$i++;
}
?>

答案 3 :(得分:0)

$list=array('first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelvelveth','thirteenth');

for( $i = 0; $i < count($list); $i++) {

    for($j = $i; $j < count($list); $j++) {
        echo $list[$j];
    }

    for($j = 0; $j < $i; $j++) {
        echo $list[$j];
    }
}

答案 4 :(得分:-1)

shift()将删除数组的第一个条目: http://php.net/manual/fr/function.array-shift.php

编辑: 你编辑过我。

print_r($list);
$tmp = array_shift($list);
$list[] = $tmp;
print_r($list);