Skip to content

Instantly share code, notes, and snippets.

@daif
Created October 4, 2020 09:33
Show Gist options
  • Save daif/3d2a76a1b48135df1326e0e081dc9278 to your computer and use it in GitHub Desktop.
Save daif/3d2a76a1b48135df1326e0e081dc9278 to your computer and use it in GitHub Desktop.
Get random elements from array
<?php
// Get array elements from items file
$items = file('items.txt');
// Pick a random element from array
$item = array_rand($items);
// Save chosen element to chosen file
file_put_contents('items-chosen.txt', $items[$item], FILE_APPEND);
// print the chosen element
print $items[$item];
// unset the chosen element
unset($items[$item]);
// save array after remove the chosen element
file_put_contents('items.txt', implode('', $items));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment