rockinaway Posted May 31, 2011 Share Posted May 31, 2011 I want to loop through all the values of an array.. but each time, JUST FOR THE LOOP, i want to go through the array values randomly. How do you suggest I do this? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2011 Share Posted May 31, 2011 How about making a copy of the array and shuffle() it, then use that in the foreach() loop? $new_array = $old_array; shuffle($new_array); foreach( $new_array as $k => $v) { // etc . . . Quote Link to comment Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 Do you want to loop through them randomly or simply echo them randomly. Perhaps if I more clearly understood what you are trying to do Quote Link to comment Share on other sites More sharing options...
rockinaway Posted June 1, 2011 Author Share Posted June 1, 2011 I was thinking the same as Pikachu2000. Basically, if I have $array1. And it has 5 values. I want to loop through these values in a RANDOM order but retain their order in $array1. So as Pikachu2000 stated, making a copy of the array to say $array2, shuffling it and then looping through it (not necessarily echoing anything) would work as then I can return to $array1 with everything preserved in the correct order. I just want to know if there is a simpler way.. Quote Link to comment Share on other sites More sharing options...
revraz Posted June 1, 2011 Share Posted June 1, 2011 Simpler? Looks pretty simple as is, how much simpler do you want it? Quote Link to comment Share on other sites More sharing options...
fugix Posted June 1, 2011 Share Posted June 1, 2011 I was thinking the same as Pikachu2000. Basically, if I have $array1. And it has 5 values. I want to loop through these values in a RANDOM order but retain their order in $array1. So as Pikachu2000 stated, making a copy of the array to say $array2, shuffling it and then looping through it (not necessarily echoing anything) would work as then I can return to $array1 with everything preserved in the correct order. I just want to know if there is a simpler way.. there are other ways to do it, but as far as simplicity is concerned, the example that Pikachu2000 provided is what you want Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.