gpt4 book ai didi

php - 如何在每次迭代中以从上到下的方式旋转数组元素

转载 作者:行者123 更新时间:2023-11-28 23:24:10 24 4
gpt4 key购买 nike

我有一个 n 大小的数组假设我的原始数组是:

array=(alpha,bravo,charlie,delta,echo,foxtrot);

我想向左旋转上面的数组前输出第一次迭代

array=(bravo,charlie,delta,echo,foxtrot,alpha); 

和第二次迭代

array=(charlie,delta,echo,foxtrot,alpha,bravo);

我想在每次迭代中都这样做,直到获得原始数组。

注意:上面的数组是我从特定查询的 MySQL 输出中获取的。所以原始数组将始终是 array=(alpha,bravo,charlie,delta,echo,foxtrot);

提前感谢您的任何建议和帮助

最佳答案

$array = array('alpha','bravo','charlie','delta','echo','foxtrot');

for($i=0; $i< count($array);$i++)
{
$firstValue = array_shift($array);
array_push($array, $firstValue);
var_dump($array); //here you get your array with the first value shifted to the end of the array
}

结果:

array(6) {
[0]=>
string(5) "bravo"
[1]=>
string(7) "charlie"
[2]=>
string(5) "delta"
[3]=>
string(4) "echo"
[4]=>
string(7) "foxtrot"
[5]=>
string(5) "alpha"
}
array(6) {
[0]=>
string(7) "charlie"
[1]=>
string(5) "delta"
[2]=>
string(4) "echo"
[3]=>
string(7) "foxtrot"
[4]=>
string(5) "alpha"
[5]=>
string(5) "bravo"
}
array(6) {
[0]=>
string(5) "delta"
[1]=>
string(4) "echo"
[2]=>
string(7) "foxtrot"
[3]=>
string(5) "alpha"
[4]=>
string(5) "bravo"
[5]=>
string(7) "charlie"
}
array(6) {
[0]=>
string(4) "echo"
[1]=>
string(7) "foxtrot"
[2]=>
string(5) "alpha"
[3]=>
string(5) "bravo"
[4]=>
string(7) "charlie"
[5]=>
string(5) "delta"
}
array(6) {
[0]=>
string(7) "foxtrot"
[1]=>
string(5) "alpha"
[2]=>
string(5) "bravo"
[3]=>
string(7) "charlie"
[4]=>
string(5) "delta"
[5]=>
string(4) "echo"
}
array(6) {
[0]=>
string(5) "alpha"
[1]=>
string(5) "bravo"
[2]=>
string(7) "charlie"
[3]=>
string(5) "delta"
[4]=>
string(4) "echo"
[5]=>
string(7) "foxtrot"
}

关于php - 如何在每次迭代中以从上到下的方式旋转数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com