PHP Arrays Randomizing an Array - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Arrays Randomizing an Array - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, May 20, 2019

PHP Arrays Randomizing an Array

PHP Arrays




Randomizing an Array

Problem

You want to scramble the elements of an array in a random order.

Solution

Use shuffle():

             shuffle($array);

Discussion

It’s surprisingly tricky to properly shuffle an array. In fact, up until PHP 4.3, PHP’s shuffle() routine wasn’t a truly random shuffle. It would mix elements around, but certain combinations were more likely than others.

Therefore, you should use PHP’s shuffle() function whenever possible.



No comments:

Post a Comment

Post Top Ad