[PHP] How to create a random array?

Advert test
Status
Not open for further replies.
#1
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
 
#3
Hi Louie,

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

It always displays
I am the ace
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
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($newbestwords, 2);
                        
                        // Run through the text and replaces all occurrences of $oldText
                        $text = str_replace($bestwords , $random, $text);
                        
                        // Display the new text
                        echo $text;
                   }
 

Forbairt

Portfolio Building
#5
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 ???
$random = array_rand($newbestwords, 2);


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
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
 

Forbairt

Portfolio Building
#7
well describe what you want to happen .. and who knows :) someone might help more .. unless louie's version already does the job
 
#8
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);
 
Status
Not open for further replies.
Top