The Little Guy Posted June 14, 2007 Share Posted June 14, 2007 Anyone know why this only works sometimes? Sometimes when I run it, it creates the scrambled word, other times it just shows a blank line. <?php $wordScramble = array("apple","superman","wireless","midlife","computer","string","massive","wrench","fiber"); $randWord = array_rand($wordScramble); // Choose Random Word From array $wordArray = str_split($wordScramble[$randWord]); // Create an array from the word $strlen = strlen($wordScramble[$randWord]); // Get the strings length $strNarray = array(); // Create an empty array do{ $rSpot = array_rand($wordArray); // Get a random letter from our letters if(!in_array($wordArray[$rSpot],$strNarray)){ array_push($strNarray,$wordArray[$rSpot]); // If the letter isn't in the empty array add it } }while(count($strNarray)<$strlen); // If the empty array is sorter than the strlen do loop again foreach($strNarray as $letter){ echo $letter; // echo out each letter one at a time } echo"\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/55647-word-scramble/ Share on other sites More sharing options...
KrisNz Posted June 14, 2007 Share Posted June 14, 2007 Does this do the same thing as what you're trying? $wordScramble = array("apple","superman","wireless","midlife","computer","string","massive","wrench","fiber"); $randWord = array_rand($wordScramble); $word_array = str_split($wordScramble[$randWord]); shuffle($word_array); $scrambled = implode("",$word_array); echo $scrambled; Quote Link to comment https://forums.phpfreaks.com/topic/55647-word-scramble/#findComment-274982 Share on other sites More sharing options...
The Little Guy Posted June 14, 2007 Author Share Posted June 14, 2007 Yup! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/55647-word-scramble/#findComment-274985 Share on other sites More sharing options...
KrisNz Posted June 14, 2007 Share Posted June 14, 2007 cool, upon looking up the manual, i see theres actually a function for shuffling a string! So you can replace lines 3 through 6 with echo str_shuffle($wordScramble[$randWord]); Moral of the story - read the manual Quote Link to comment https://forums.phpfreaks.com/topic/55647-word-scramble/#findComment-275001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.