I have an array and want to use the results from a loop as a variable.
Example:
$array = array ( '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' );
srand (time());
$key1=rand(2,5); // Chooses a number between 2 and 5
$randresult = array_rand($array,$key1);
$i=0;
while ($i < $key1) {
echo $array[$randresult[$i]] ."<br> \n"; // ******MY PROBLEM IS HERE
$i++; }
What I really want are the 2 to 5 results of the array stored as a variable so I can use them elsewhere.
Only the following command works---> echo $array[$randresult[$i]] ."<br> \n";
(That prints it on the screen and doesn't create a variable for it)
when I try:
$savedvariable= "$array[$randresult[$i]] .<br> \n";
(I tried this with and without the period(.))
I get a syntax error.
I get nothing when I try this as well:
$savedvariable= "$array($randresult[$i]) .<br> \n";
(I tried this with and without the period(.))
How do I save the result of the random array as a variable?