Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.