Jump to content

Need help with function variables and how to get them in an array


drew7721

Recommended Posts

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

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

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

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.