Skip to content

Instantly share code, notes, and snippets.

@hkdobrev
Created March 20, 2012 12:47
Show Gist options
  • Save hkdobrev/2134941 to your computer and use it in GitHub Desktop.
Save hkdobrev/2134941 to your computer and use it in GitHub Desktop.
randomize arrays
<?php
function randomize($initial, $to_merge = NULL)
{
$result = array();
$lengths = array();
$total = 0;
$arrays = func_get_args();
foreach ($arrays as $array)
{
$count = count($array);
$total += $count;
$lengths[] = $count;
}
$shuffled = range(0, $total - 1);
shuffle($shuffled);
foreach ($shuffled as $number)
{
$step = 0;
foreach ($lengths as $key => $length)
{
if ($number < $step + $length)
{
$result[] = $arrays[$key][$number - $step];
break;
}
$step += $length;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment