Created
October 4, 2020 09:33
-
-
Save daif/3d2a76a1b48135df1326e0e081dc9278 to your computer and use it in GitHub Desktop.
Get random elements from array
This file contains 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 | |
// 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