-2

我在用:

$input = array("1", "2", "3", "4", "5");

现在我想首先打印数组 (1) 中的第一个值和剩余的值 (2,3,4,5) 的另一个值。然后下一次,打印值将是 2 和更多值 (3,4,5,1)。以此类推,直到打印值为 5,其他值为 (1,2,3,4)。

我正在使用 for 循环,但在值 5 之后,循环应该中断......

这种情况该怎么办......

输出应该是:第一次,然后是 2,然后是 3,然后是 4,然后是 5,然后是 1,然后在每次刷新时继续

4

5 回答 5

2

这是否符合您的要求?

<?php

$input = array("1", "2", "3", "4", "5");
$arr = array();

foreach ($input as $item) {
    $i = array_shift($input);
    $arr[] = $input;
    $input[] = $i;
}

print_r($arr);

输出

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 5
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 1
        )

    [2] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 1
            [3] => 2
        )

    [3] => Array
        (
            [0] => 5
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [4] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
)
于 2012-05-04T05:36:47.123 回答
1

你要这个:

<?php 
session_start();

if(!isset($_SESSION['loop_num'])){
   $_SESSION['loop_num'] = 1;
}

$loop = true;

echo "Current: {$_SESSION['loop_num']} <br>";
$i = $_SESSION['loop_num'];

while($loop === true) {
    if($i >= 5){
        $i = 1;
    } else {
        $i++;
    }

    if($_SESSION['loop_num'] == $i){
        $loop = false;
        break;
    }
    $others[] = $i;

} 

if($_SESSION['loop_num'] >= 5){
        $_SESSION['loop_num'] = 1;
    } else {
        $_SESSION['loop_num']++;
    }
print_r($others);
?>

输出:

Current: 4 
Array ( [0] => 5 [1] => 1 [2] => 2 [3] => 3 )

Current: 5 
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

ETC..

于 2012-05-04T06:09:20.443 回答
0

尝试实施此解决方案:

$size = count($input);
$input = array_merge($input,$input);
for($i=0;$i<$size;$i++){
    $output = array_slice($input,$i,$size-1);
    print_r($output);
}
于 2012-05-04T05:41:13.363 回答
0

如果您只想要一个永远循环的不断旋转的数组,则可以使用:

$array = range(1,5);

foreach($array as &$number) {
    echo $number . PHP_EOL;
    array_push($array, array_shift($array));
}

如果您希望它在每次页面加载时旋转,将前面的数字设置为一个变量,其余的在专用数组中排成一行,这样可以:

session_start();

if(!$_SESSION['loop_array']) {
    $_SESSION['loop_array'] = range(1,5);
} 
    $current_value = array_shift($_SESSION['loop_array']);
    $others_values = $_SESSION['loop_array'];

    // Push current value to back of rotation, leaving next in line for
    // next page load.
    array_push($current_value, $_SESSION['loop_array']);

这也适用于以下数组(或任何数组):

$_SESSION['cute'] = array("dog", "cat", "pony", "bunny", "moose");

$_SESSION['ordinals'] = array("first", "second", "third", "fourth", "fifth");

$_SESSION['tick_tock_clock'] = array("I", "II", "III", "IV", "V", "VI", 
                                     "VII", "VIII", "IX", "X", "XI", "XII");

$_SESSION['randomness'] = array('butter', 'pillow', 'Alabama', 'bleeding gums');
于 2012-05-04T06:00:26.273 回答
-2
<?php 

$input = array("1", "2", "3", "4", "5");

while(!empty($input)) {

    foreach($input as $i) {

        var_dump($i);

    }

    $input = array_splice($input, 0, count($input) - 1);

}

?>
于 2012-05-04T05:45:48.960 回答