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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

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

Post by shiznatix »

d11wtq wrote:I am having "one of those days"
its monday, so your excused
Post Reply