Jump to content

Word Scramble


The Little Guy

Recommended Posts

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";
?>

Link to comment
https://forums.phpfreaks.com/topic/55647-word-scramble/
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/55647-word-scramble/#findComment-274982
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.