PHP Array randomization
Oct
18
I’m a bit perplexed why you can sort an array with keys in PHP and you can randomize an array with no keys, but you cannot randomize an array with keys. Weird…
If you have to randomize an array with keys, here is a way to do it. Would have been nice to have been able to simply say:
shuffle($arrayWithKeys);
but thats not to be as you would lose the keys.
So what you need to do is:
//Make an array without keys of the keys of your associative array
$keys = array_keys($arrayWithKeys);
//Shuffle the keys up
shuffle($keys);
//New array which is the one you would use or return
$newArrayWithKeys = array();
foreach($keys as $k) {
//The array items would be packed into the new array in the order that it was shuffled.
$newArrayWithKeys[] = $arrayWithKeys[$key];
}
Thanks to Sajith for the tip!
2 Comments to "PHP Array randomization"
Spit it out!
PHP, Programming Stii
Recent Posts
- Afrigator vs Regator
- Don’t pirate music/movies! You might be forced to use Windows if you do…
- Pike > Python?
- Using Twhirl for FriendFeed
- Being anti-social SUCKS!
- Example using XFN and Optimus to find friends on Afrigator
- Optimus, the ultimate Microformats parser
- Please support UnitedForAfrica.co.za
- Afrigator Beta2.0 - Lets amplify the Xenophobia issue
- What can we as bloggers do about the Xenophobia crisis?
My Posse
- Jayx’s bloggy
- Gogo’s blog
- Go2 South Africa
- Stumble Upon
- Dave Duarte
- Wikipedia
- zlythern
- Max Kaizen
- Tresblue
- Mike Stopforth
- RafiQ
- Muti.co.za
- Employmint
- Danette’s Bloggy!
- Thinking Machine
- White African
- kiefpiet.co.za
- Skuff’s World
- Goozeberry
- Crossloop blog
- Crossloop
- Aquila Online
- Charl van Niekerk
- Derek Allard
- Code Igniter
- Carls
- Justin Hartman
- blik.co.za
- Stefano Sessa
- Uno de Waal
- Amplitude!
- bLaugh
- Tyler Reed
- Chris Rawlinson
- Stormhoek!
- 3am
- Mike Solomon
- Mobile Q and A
- Eric Edelstein
- Marc Forrest
- Imel Rautenbach
- Absolutewillie
- Vincent Maher
- Colin Daniels
- Groogle!
- Chilibean
- Paul Jacobson
- Ayelet
- Python Guru Neil
- Rails Guru Nic
- Beverley Merriman
- Miguel
- Nic Harrywhatshisname
- Chris iMod
- Geekrebel!
- Steven McD
- Belinda sweetheart!
- Henre Rossouw
- JPGeek
- Foxinni
- Adii
- Charl Norman
- Bandwidthblog
- Jason Bagley
- Simon Botes
- Auric Silverwing
- Mark Forrester
- Saul Kropman
- Fred Roed
- Sass Schultz
Filed in
- Afrigator (26)
- ajax (9)
- API (2)
- Apple stuff (10)
- Blogging (24)
- browsers (5)
- Business (27)
- Code Igniter (8)
- firefox (8)
- flock (14)
- Funnies (72)
- GeekDinner! (18)
- General and sometimes Rants (47)
- Go2SA (2)
- ideas 2.0 (14)
- javascript (12)
- Kick-ass Tools (30)
- Linux (5)
- Marketing (25)
- moo.ajax (4)
- mootools (6)
- Open Source (10)
- Programming (33)
- C# (1)
- PHP (13)
- Python (9)
- Ruby (on Rails) (9)
- RSS (5)
- Semantic Web (32)
- Social Web (57)
- Software Development (15)
- South Africa (33)
- Tagging (6)
- Techie stuff (22)
- Tshirts (2)
- Tutorials (42)
- Blogging (17)
- Flocking (6)
- muti.co.za (13)
- Web 2.0 (73)
- web development (20)
Past Stuff
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006















I love this sentence: “you can randomize an array with no keys, but you cannot randomize an array with keys.” - had to read that a couple times
@Chris:
yeah you’re right! Quite a tongue twister isn’t it!