Created
March 20, 2012 12:47
-
-
Save hkdobrev/2134941 to your computer and use it in GitHub Desktop.
randomize arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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