Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Created November 10, 2018 18:08
Show Gist options
  • Save Frago9876543210/7881efe063855c2bf0e00464f2229eba to your computer and use it in GitHub Desktop.
Save Frago9876543210/7881efe063855c2bf0e00464f2229eba to your computer and use it in GitHub Desktop.
<?php
$array = range(0, 19);
luck($array, 0.1, function(int $element) : void{
echo "*** jack pot / $element / with 0.1\n";
});
luck($array, 0.9, function(int $element) : void{
echo "* $element / with 0.9\n";
});
luck($array, null, function(int $element) : void{
echo "\t$element not processed!\n";
});
function luck(array &$array, ?float $percent, callable $callback){
if($percent === null){
foreach($array as $value) $callback($value);
return;
}
$keys = array_rand($array, $count = (int) count($array) * $percent);
shuffle($keys);
foreach($keys as $key){
$callback($array[$key]);
unset($array[$key]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment