Home > php > Randomize php array …

Randomize php array …

Hi Guys ,

Here is a function to randomize php array .


function RandomizeArray($array){

// error check:
$array = (!is_array($array)) ? array($array) : $array;

$a = array();
$max = count($array) + 10;

while(count($array) > 0){
$e = array_shift($array);
$r = rand(0, $max);

// find a empty key:
while (isset($a[$r])){
$r = rand(0, $max);
}
$a[$r] = $e;
}

ksort($a);
$a = array_values($a);

return $a;
}

Advertisement
Categories: php
  1. December 4, 2008 at 2:37 pm | #1

    very nice.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.