I have the following code, it selects a random thing from the array (in this case a hex code) and then adds it to the variable $color, however I have 1.7K hex codes I need to add, which is gonna be very time consuming, so is there anyway I can make it work with a .txt file instead? <?php $array = array( 'FFF', 'FF0000', '000' ); $numbers = array(); $rand = rand(0, count($array)-1); function getrand($rand, $numbers, $array) { while (in_array($rand, $numbers)) $rand = rand(0,count($array)-1); return $rand; } $n = getrand($rand, $numbers, $array); array_push($numbers, $n); $color = $array[$n]; ?> PHP: Help appreciated!
http://us.php.net/file Check out the file function. It reads a file into an array suing new lines as a delimeter.
Galen is correct you can just read from a file, but if thats done on every page load itll be slow What are you trying to achieve? There might be a simpler way.
I got this: <?php $lines = file('hex.txt'); foreach ($lines as $line) { echo "'" . htmlspecialchars($line) . "'<br />\n"; } ?> PHP: I was thinking if I managed to spit out 'hex', I could put that it into an array, but there's always a space on the end before the ',
sorry dude i dunno how your file is constructed OR why on earth you need 17thousand different hex values I'd personally find a common factor between the values then do it programmatically. I.e. if you just needed ~17k colours, you could quite easily make a script to generate colours based on the fact.
1700 colours, I'm randomising the background, I could cut it down alot I guess though, but I'd still have this problem.
Try this instead creates random number from 0-255 converts it to hex then adds 0 before if necessary.