[PHP] How to create a random array?

Discussion in 'Coding Help' started by babyboy808, Dec 29, 2007.

Tags:
  1. babyboy808

    babyboy808 New Member

    Hi,

    How would I go about randomizing an array in PHP?

    Basically I have an array of a few elements and on each call I want to output a different element if that makes sense :)

    Here's where I'm at:

    Code:
                       for($i=0; $i<=$text; $i++)
                       {
                            
                            // The word we want to replace
                            $bestwords = array('best');
                            
                            // The new word we want in place of the old one
                            $newbestwords = array('ace', 'coolest', 'finest', 'incomparable');
                            
                            // Randomize array
                            $random = array_rand($newbestwords, 2);
                            
                            // Run through the text and replaces all occurrences of $oldText
                            $text = str_replace($bestwords , $newbestwords , $text);
                            
                            // Display the new text
                            echo $text;
                       }
    Most of the code is ripped from different sources.

    thanks in advance,
    Keith
     
  2. louie

    louie New Member

    That code should work but I need to see where are you using it.

    Best put the code in a function and call it when need it.
     
  3. babyboy808

    babyboy808 New Member

    Hi Louie,

    That's where I'm at so far with everything.

    It always displays
    when run and never any other of the words?

    I haven't a clue what's happening.

    Also what is this bit here:
    Code:
    $random = array_rand($newbestwords, 2);
     
  4. louie

    louie New Member

    try this:
    PHP:
    for($i=0$i<=$text$i++)
                       {
                            
                            
    // The word we want to replace
                            
    $bestwords = array('best');
                            
                            
    // The new word we want in place of the old one
                            
    $newbestwords = array('ace''coolest''finest''incomparable');
                            
                            
    // Randomize array
                            
    $random array_rand($newbestwords2);
                            
                            
    // Run through the text and replaces all occurrences of $oldText
                            
    $text str_replace($bestwords $random$text);
                            
                            
    // Display the new text
                            
    echo $text;
                       }
     
  5. Forbairt

    Forbairt Portfolio Building

    ok this seems a bit weird ...

    you are initializing your arrays each time in your loop ? they should be outside of it ...

    you are also picking 2 entries from the array placing it into another array ???


    What is your original text string ?

    "I am the best? " ???


    You are iterating / trying to iterate through a piece of text ??

    thats not making sense to me ?

     
  6. babyboy808

    babyboy808 New Member

    Hi Forbairt, thanks for the reply.

    You have obviously picked up on my crap php "skills" :) - I don't know how to code it exactly, but I know what I want it to do, I guess I haven't described it well enough.. oh well
     
  7. Forbairt

    Forbairt Portfolio Building

    well describe what you want to happen .. and who knows :) someone might help more .. unless louie's version already does the job
     
  8. louie

    louie New Member

    it should but as I already said it will work better in a function and without the "loop" or
    PHP:
    $bestwords = array('best');
    $newbestwords = array('ace''coolest''finest''incomparable');                        
    echo 
    str_replace($bestwords array_rand($newbestwords,1), $text);
     

Share This Page