makka Posted April 23, 2007 Share Posted April 23, 2007 well hello. i would like to know if you can help me with something i have a variable that ill call variableA and i want a loop or something like that to try and reconise it so it will loop through trying to match it eg the following code is totally wrong just explain what i want $variableA = "hello" if (variableA == a) { echo "hello"; } elseif (variableA == b) { echo "matched"; } elseif (variableA == c) { echo "matched"; } elseif (variableA == d) { echo "matched"; } elseif (variableA == e) { echo "matched"; } elseif (variableA == f) { echo "matched"; } elseif (variableA == g) { echo "matched"; } elseif (variableA == h) { echo "matched"; } elseif (variableA == i) { echo "matched"; } // so on untill z then it woulc be like elseif (variableA == aa) { echo "matched"; } elseif (variableA == bb) { echo "matched"; } //so on an on an on get what i want? how can i do it? its for a guessing game Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 erm.. the echo is always hello so i am confused.. maybe a switch.. ??? Quote Link to comment Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 <?php $answerArray = array("a" => "A!", "b" => "B!", "c" => "C!", "d" => "D!"); $variableA = "c"; foreach ($answerArray as $selection => $answer) { if ($selection == $variableA) { print $answer; break; } } ?> Something like that would work. Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 erm.. the echo is always hello so i am confused.. maybe a switch.. ??? as you wish Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 <?php $answerArray = array("a" => "A!", "b" => "B!", "c" => "C!", "d" => "D!"); $variableA = "c"; foreach ($answerArray as $selection => $answer) { if ($selection == $variableA) { print $answer; break; } } ?> Something like that would work. and with this it just gets the first letter what is the ! for "a" => "A!" Quote Link to comment Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 The ! is nothing, that is the value you want to print out. It could of been this: <?php $answerArray = array("a" => "You selected A!", "b" => "You selected B!", "c" => "You selected C!", "d" => "You selected D!"); $variableA = "c"; foreach ($answerArray as $selection => $answer) { if ($selection == $variableA) { print $answer; // prints You selected C! break; // remove this if you want the loop to go through every element of the array. } } ?> Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 so if $variableA = "hello lol"; it would loop through untill it knew that then echo out "hello lol" < another words the answer Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 any one Quote Link to comment Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 I think you are missing the point of the array. www.php.net/array The array seen above is considered an associative array. The first part IE: "a" => is the KEY of the array, meaning you can acesses the second part or the value "You selected A!" by calling $array["a"] and that will print out "You selected A!" So if $variableA = "a"; it will print out "You selected A!" using the loop I created above. If $variableA = "c"; it will print out "You selected C!"; It works the same as the following: <?php $answerArray = array("a" => "You selected A!", "b" => "You selected B!", "c" => "You selected C!", "d" => "You selected D!"); $variableA = "c"; print $answerArray[$variableA]; // Prints "You selected C!" since the index of "c" value is "You Selected C!"; ?> I would highly suggest you read up on arrays and how they are used. Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 done that just i want it to try and match a full string like a word then print out that word Quote Link to comment Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 Maybe you are looking for something similiar to this ??? <?php $mainArticle = 'Computer programming is one of the best for certain php languages that are not of this world! that'; $articles[0] = 'This is a topic that is totally not related to the first article at all!'; $articles[1] = 'Programming in PHP has done miraculous wonders to this time and many other exciting events!'; $articles[2] = 'Programming This is a topic that is totally not related to the first article at all'; $articles[3] = 'Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all'; $articles[4] = 'Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages'; $articles[5] = 'Jack and jill went up the hill to fetch a pail of water, jack fell down and broke his crown and jill came tumbling after!'; $related = relatedTest($mainArticle, $articles); print "The following articles are related to : " . $mainArticle . " (ordered by most revlevant)<br /><br />"; foreach ($related as $key => $matches) { print "Article: " . $articles[$key] . "<br />"; } print "<br /><br /><br />These were all the articles used.<br /><br />"; foreach ($articles as $article) { print $article . "<br />"; } function relatedTest($mainArticle, $articles) { $mainArticle = stripCommons($mainArticle); $words = explode(" ", $mainArticle); foreach ($articles as $key => $article) { $artWords[$key] = explode(" ", stripCommons($article)); $matches = compareWords($words, $artWords[$key]); if ($matches > 0) { $match[$key] = $matches; }else { unset($artWords[$key]); } } arsort($match); return $match; } function compareWords($words, $compwords) { $match = 0; if (is_array($words)) { foreach ($words as $word) { foreach ($compwords as $compword) { if (strtolower($compword) == strtolower($word)) { $match++; } } } } return $match; } function stripCommons($article) { $article = ereg_replace("'|\.|\?|!|,|\"|&|:|-|\[|\]|\(|\)|\+|=|~|\||\*|\^|%|\$|@|#|<|>|`|;|_|\{|\}", "", $article); $article = " " . $article . " "; $commonWords = array("if", "u", "so", "it", "its", "is", "of", "or", "by", "on", "but", "a", "was", "for", "it", "this", "was", "to", "are", "can", "you", "your", "any", "or", "the", "with", "this", "not", "at", "and", "that"); $commonWords = strlenSort($commonWords); foreach ($commonWords as $word) { if (eregi(" ".$word." ", $article)) { $article = str_replace(" ".$word." ", " ", $article); } } return trim($article); } function strlenSort($array) { // sort array by string length foreach ($array as $key => $size) { $newArray[$key] = strlen($size); } arsort($newArray, SORT_NUMERIC); $i=0; foreach ($newArray as $key => $size) { $returnArr[$i++] = $array[$key]; } return $returnArr; } ?> Which when ran would produce: he following articles are related to : Computer programming is one of the best for certain php languages that are not of this world! that (ordered by most revlevant) Article: Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages Article: Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all Article: Programming in PHP has done miracuouls wonders to this time and many other exciting events! Article: Programming This is a topic that is totally not related to the first article at all These were all the articles used. This is a topic that is totally not related to the first article at all! Programming in PHP has done miracuouls wonders to this time and many other exciting events! Programming This is a topic that is totally not related to the first article at all Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages Jack and jill went up the hill to fetch a pail of water, jack fell down and broke his crown and jill came tumbling after! Quote Link to comment Share on other sites More sharing options...
makka Posted April 23, 2007 Author Share Posted April 23, 2007 thanks ill try and work off of it ive just been given a high priority thing for my alliance in ogame ... so ill call it solved :-\ Quote Link to comment 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.