0

这是我的数组

$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

以此类推。

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

4

5 回答 5

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/>';

}
于 2013-07-19T08:41:45.980 回答
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++;
}
?>
于 2013-07-19T09:06:59.280 回答
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];
    }
}
于 2013-07-19T09:14:32.130 回答
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 />";
}
?>
于 2013-07-19T08:41:24.263 回答
-1

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

编辑:你编辑了,我也编辑了。

print_r($list);
$tmp = array_shift($list);
$list[] = $tmp;
print_r($list);
于 2013-07-19T08:36:23.617 回答