Skip to content

Instantly share code, notes, and snippets.

@pwaldhauer
Created December 10, 2014 20:50
Show Gist options
  • Save pwaldhauer/4fef8ca0875ac0acd18d to your computer and use it in GitHub Desktop.
Save pwaldhauer/4fef8ca0875ac0acd18d to your computer and use it in GitHub Desktop.
<?php
function array_swap(&$array,$swap_a,$swap_b){
list($array[$swap_a],$array[$swap_b]) = array($array[$swap_b],$array[$swap_a]);
}
$zwerge = [5, 5, 5, 5, 5, 8, 8, 8, 8, 8];
shuffle($zwerge);
print_r($zwerge);
for($i = 0; $i < count($zwerge) - 1; $i++) {
$self = $zwerge[$i];
for($o = 1; $o < count($zwerge) - 1; $o++) {
$first = $zwerge[$o];
$second = $zwerge[$o+1];
if($first != $second) {
array_swap($zwerge, $o + 1, $i);
break 1;
}
}
}
echo "done\n";
print_r($zwerge);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment