Array help needed

Discussion in 'PHP' started by crazyryan, Aug 28, 2007.

  1. #1
    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! :)
     
    crazyryan, Aug 28, 2007 IP
  2. Galen

    Galen Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    http://us.php.net/file

    Check out the file function. It reads a file into an array suing new lines as a delimeter.
     
    Galen, Aug 28, 2007 IP
  3. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #3
    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.
     
    m0nkeymafia, Aug 28, 2007 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    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 ',
     
    crazyryan, Aug 28, 2007 IP
  5. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #5
    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.
     
    m0nkeymafia, Aug 28, 2007 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    1700 colours, I'm randomising the background, I could cut it down alot I guess though, but I'd still have this problem.
     
    crazyryan, Aug 28, 2007 IP
  7. Galen

    Galen Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try this instead

    creates random number from 0-255 converts it to hex then adds 0 before if necessary.
     
    Galen, Aug 28, 2007 IP