samedi 11 novembre 2017

Write Text File From Random Array Results (PHP)

This is my first question so please let me know if I've done anything wrong.

Below is a script I'm working on with the intend of simple taking a large list on a text file, shuffling it, then randomly choosing 5 items and writing a new file with those 5 items.

I've tried the last step many different ways. Put, fopen, fwrite, etc.

<?php

 // create array from text file
 $array = file('master_file.txt');

 // shuffle the array
 shuffle ($array);

// clean up spaces
$array = array_filter(array_map('trim', $array));

// select 5 random items and echo them
$rand_keys = array_rand($array, 5);
echo $array[$rand_keys[0]] . "\n";
echo $array[$rand_keys[1]] . "\n";
echo $array[$rand_keys[2]] . "\n";
echo $array[$rand_keys[3]] . "\n";
echo $array[$rand_keys[4]] . "\n";

// write new text file with same results

 here is where I am having trouble

 ?> 




Aucun commentaire:

Enregistrer un commentaire