Rate www.sajithmr.com Sajithmr.com
  • Perfect
    Perfect(32.6%)
  • Good
    Good(47.2%)
  • Bad
    Bad(9.6%)
  • Too Bad
    Too Bad(10.6%)


Php Randomize Array (Shuffle)

images.jpg

If you want to randomize a php array , you can use shuffle function.

But we can’t randomize the whole array with its value, it might be integer, string or collection of objects etc. So we randomize or shuffle the keys of the array.

Eg:

$Keys = array_keys($MyArray);
shuffle($Keys);

$NewArray = array(); //declare a new array
foreach( $Keys as $key)

{

$NewArray[] = $MyArray[$key];

}

Here the NewArray will be the shuffle of MyArray

Thanks

Sajith.M.R


Post a Comment


Popular Posts

Recent Comments

Subscribe

 Subscribe in a reader Or, subscribe via email:
Enter your email address:  



Tag Cloud