Page 1 of 1

Have I missed something? array_rotate() ????

Posted: Mon May 02, 2005 9:40 am
by Chris Corbyn
Hi,

Maybe I'm just being plain crazy or something but I swear I have used a function before called something to the effect of array_rotate()....

As in, it rotates the array contents (take the first element off and puts it on the end). I can't find it now so I'm doing this...

Please tell me if there IS a PHP function for this (mine only does numeric indexes (but I could change it)) :? I am having "one of those days" :P

Code: Select all

function array_rotate($array, $rev=false) {
    if ($rev) {
        array_reverse($array); //Back-to-front
    } //End if
    $first = $array[0]; //Store first value
    array_shift($array); //Take off first value
    array_push($array, $first); //Put first back on the end
    if ($rev) {
        array_reverse($array); //Back-the-right-way
    } //End if
    return $array;
} //End function

Posted: Mon May 02, 2005 10:27 am
by pickle
I think you're looking for array_reverse()

Edit:
Wait... maybe I spoke to soon. What does this mythical function do after it moves the first element to the end? Just stop?

Posted: Mon May 02, 2005 11:13 am
by Chris Corbyn
Yeah it just stops. You have to repeat the operation if you want to move things further around.

It must have either been a different language or something I've dreamed about :P since I now need to use it lol.

Oh well.. if anybody happens to need such a function use the one above (putting $rev=true makes it take the end value and put it on the beginning). If you want one that supports assoc arrays I'll change it.

I could swear I've seen this somewhere (I even thought I knew what it was called... scary).

Re: Have I missed something? array_rotate() ????

Posted: Mon May 02, 2005 12:56 pm
by shiznatix
d11wtq wrote:I am having "one of those days"
its monday, so your excused