Skip to content

Instantly share code, notes, and snippets.

@Noxalus
Last active August 29, 2015 14:14
Show Gist options
  • Save Noxalus/37a4e7a40ac70037f6b2 to your computer and use it in GitHub Desktop.
Save Noxalus/37a4e7a40ac70037f6b2 to your computer and use it in GitHub Desktop.
Take 4 random values in an array with no duplicate entry
<?php
$array = array(
'42' => 'COUCOU1',
'87' => 'COUCOU2',
'38' => 'COUCOU3',
'98' => 'COUCOU4',
'74' => 'COUCOU5',
'16' => 'COUCOU6'
);
$copy = $array;
$random_keys = array();
while(count($random_keys) < 4)
{
$random_key = array_rand($copy);
$random_keys[] = $random_key;
unset($copy[$random_key]);
}
echo '<pre>';
print_r($random_keys);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment