This is a discussion on [PHP] How to create a random array? within the Coding Help forums, part of the Webmaster Discussion category; Hi, How would I go about randomizing an array in PHP? Basically I have an array of a few elements ...
| |||||||
| Register | Forum Rules | FAQ | Donate | Calendar | Search | Today's Posts | Mark Forums Read |
| |||||
| 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;
}
thanks in advance, Keith |
| |||||
| 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.
__________________ :. Web Design & Development Web Design Ireland :. Search Engines Optimization Search Engines Optimization :. Car Parts & Accessories Car Parts :. Cars Ireland Cars Ireland :. I Have 2 Find It Directory SEF Directory |
| |||||
| Hi Louie, That's where I'm at so far with everything. It always displays Quote:
I haven't a clue what's happening. Also what is this bit here: Code: $random = array_rand($newbestwords, 2); |
| |||||
| try this: PHP Code: |
| |||||
| 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 ??? Quote:
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 ?
__________________ IT Books.ie | Computer Books Recent Work | The Dropped.IE Domain List | Devine Building | Children's Festivals in Ireland | Galway Hot Yoga | Freelance Jobs African Safari Holiday Blog | James Larkin Work | Web Design Dublin, Ireland - Web Development Dublin, Ireland |
| |||||
| Hi Forbairt, thanks for the reply. You have obviously picked up on my crap php "skills" |
| |||||
| it should but as I already said it will work better in a function and without the "loop" or PHP Code: |
| Tags |
| array, create, php, random |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| | ![]() | |||
| | ![]() | |||