繁体   English   中英

PHP遍历数组。 将第一个元素移到最后一个

[英]PHP Cycle through array. Move the first element to the last

只是想如果有人发现它的用途,我想分享一下。

基本上,我需要一个HTML颜色列表来循环/循环。 因此,我需要删除数组的第一个元素,将其放在最后,然后获取当前的HTML颜色。

给定以下数组:

$colors = array(  
    "#2265fa", "#b61d1e", "#53b822", "#a9d81c", "#d6e9f5", "#43cc7d", "#e3159a", 
    "#80c85e", "#17b303", "#989240", "#014c07", "#d265f3", "#22bbb9", "#6c69a9", 
    "#7ea13a", "#0dcea2", "#99c27d", "#41405b", "#731801"
);

这就是我想出的。 当然,会有数百种方法可以做到这一点。 这是我的看法。

# Array_shift returns the value it takes off the beginning of the array. 
# And I merely append this to the end of the array
$colors[] = array_shift($colors);

# Using current I am able to get the current first element of the array back
echo current($colors);

在这种情况下,将是“#b61d1e”,它是数组的当前索引。 愿您在某处觉得有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM