drew7721 Posted April 28, 2010 Share Posted April 28, 2010 Hello, I have this issue with the following code. I'm trying to generate random words from an array of given words. The problem is that I do not want to limit/force the ammount of words it needs to be able to randomize. baisically I want to be able to put : randomtext("word1","word2","word3","word4") randomtext("word5","word6") randomtext("word7","word8","word9") so that the function takes in consiteration the words no matter the ammount 2 or 20 and randomizes the output. Is there anyway to include all variables from function in the array ? here is the code : <?php $limit=10; // set how many vatriations are wanted function randomtext($value1,$value2,$value3,$value4) //setting funtion { $words = array($value1,$value2,$value3,$value4); //getting words from function $word = array_rand($words,1); // pick one random word return $words[$word]; // return picked word } if(empty($i)) { //decleare value of $i $i=1; } while($i<$limit) // do loop { echo randomtext("favorite","best","awsome","excellent").' - '.randomtext("test1","test2","test3","test4").'<br />'; //echo words $i++; } //inrease value ?> Thank you Link to comment https://forums.phpfreaks.com/topic/200076-need-help-with-function-variables-and-how-to-get-them-in-an-array/ Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 func_get_args Link to comment https://forums.phpfreaks.com/topic/200076-need-help-with-function-variables-and-how-to-get-them-in-an-array/#findComment-1050105 Share on other sites More sharing options...
mrMarcus Posted April 28, 2010 Share Posted April 28, 2010 assuming i'm following: <?php $limit=10; // set how many vatriations are wanted function randomtext($arr) //setting funtion { $word = array_rand($arr, 1); // pick one random word return $arr[$word]; // return picked word } if(empty($i)) { //decleare value of $i $i=1; } while($i<$limit) // do loop { echo randomtext($arr_one = array('favorite', 'best', 'awesome', 'excellent', 'etc')).' - '.randomtext($arr_two = array('test1', 'test2', 'test3', 'test4', 'etc')).'<br />'; //echo words $i++; } //inrease value ?> Link to comment https://forums.phpfreaks.com/topic/200076-need-help-with-function-variables-and-how-to-get-them-in-an-array/#findComment-1050109 Share on other sites More sharing options...
drew7721 Posted April 28, 2010 Author Share Posted April 28, 2010 thank you! it works! YEAH ! radom words! <?php $limit=10; // set how many vatriations are wanted function randomtext() //setting funtion { $words = func_get_args(); //getting words from function $word = array_rand($words,1); // pick one random word return $words[$word]; // return picked word } if(empty($i)) { //decleare value of $i $i=1; } while($i<$limit) // do loop { echo randomtext("favorite","best","awsome","excellent").' - '.randomtext("test1","test3","test4").'<br />'; //echo words $i++; } //inrease value ?> Link to comment https://forums.phpfreaks.com/topic/200076-need-help-with-function-variables-and-how-to-get-them-in-an-array/#findComment-1050110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.