【问题标题】:PHP Cycle through array. Move the first element to the lastPHP 循环遍历数组。将第一个元素移到最后一个
【发布时间】:2013-10-09 12:47:00
【问题描述】:

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

基本上我需要一个 HTML 颜色列表来循环/循环。所以我需要把数组的第一个元素去掉,放在最后,然后得到当前的HTML颜色。

给定以下数组:

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

【问题讨论】:

标签: php arrays cycle


【解决方案1】:

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

# 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”是数组的当前索引。愿你在某处发现这很有用。

【讨论】:

  • 可能不是合适的地方,但我仍然喜欢快速简单的解决方案。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2016-02-22
  • 1970-01-01
  • 2015-04-18
  • 2015-07-30
  • 2018-06-09
  • 1970-01-01
相关资源
最近更新 更多