Adeus Posted March 3, 2008 Share Posted March 3, 2008 Hello, I am trying to accomplish somewhat of a shuffle (for lack of better term). I am using mysql_fetch_array to populate the array. I am trying to get it to act like this; {0, 1, 2} {1, 2, 0} {2, 0, 1} then back to {0, 1, 2} etc... As you can see, the first item in the array moves to the back with each "shuffle." I have checked out array_shuffle, array_splice, etc... I can't seem to find a good way to do this. Any help is much appreciated. At first, I used a variable which increases by +1 each page load, looping from 0 to 2. I then used this number for the LIMIT ($number, 3) in my SQL query. This worked, but of course did not append the first item to the end of the list after a page load. Link to comment https://forums.phpfreaks.com/topic/94153-array-shuffling/ Share on other sites More sharing options...
priti Posted March 3, 2008 Share Posted March 3, 2008 try array_shift() but you have to manipulate with array_push() and array_pop() Link to comment https://forums.phpfreaks.com/topic/94153-array-shuffling/#findComment-482286 Share on other sites More sharing options...
aschk Posted March 3, 2008 Share Posted March 3, 2008 How about this: <?php $testArray = array(0,1,2); $times = 5; // This is the number of times you want to perform your rotation. for($i=0; $i = $times; $i++){ $tmp = array_shift($testArray); // Take value from the front. array_push($tmp); // Put front value on the back. } ?> Hope that answers your question. Link to comment https://forums.phpfreaks.com/topic/94153-array-shuffling/#findComment-482287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.