PHP: 'rotate' an array?

后端 未结 14 1804
小鲜肉
小鲜肉 2020-11-28 14:16

is it possible to easily \'rotate\' an array in PHP ?

Like this: 1, 2, 3, 4 -> 2, 3 ,4 ,1

Is there some kind of built-in PHP function for this?

14条回答
  •  -上瘾入骨i
    2020-11-28 15:01

    The logic is to swap the elements. Algorithm may look like -

     for i = 0 to arrayLength - 1
        swap( array[i], array[i+1] )     // Now array[i] has array[i+1] value and 
                                         // array[i+1] has array[i] value.
    

提交回复
热议问题