Forum Moderators: coopster
<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$one = Array();
$one[1] = "test";
$two = Array();
$two[1] = "test2";
$get = shuffle($one,$two);
echo $get;
?>
$get = shuffle($one,$two);
echo $get;
bool shuffle ( array &$array )
If you want to randomize the arrays then first you could put them together, then call array_rand [uk3.php.net] to randomize it.
<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
// dont know why you were using 2 arrays so iv converted it all into 1
$main = array();
$main[0] = 'test1';
$main[1] = 'test2';
$rand = array_rand($main);
echo $main[$rand];
// if you want more than 1 answer
$rand_2 = array_rand($main, 2);
echo $main[$rand[0]];
echo $main[$rand[1]];
?>
[edited by: PHP_Chimp at 7:13 pm (utc) on Jan. 10, 2008]
[php]<?php [1][edited by: Simone100 at 9:11 pm (utc) on Jan. 10, 2008]
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$one = Array();
$one= "test";
$two = Array();
$two[1] = "test2";
$get = shuffle($one,$two);
echo $get;
?>[/php]
PHP Code:
<? php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$one[3] = "test3";
$one[4] = "test4";
$get = array_rand($one);
echo $get;
?>
Instead of showing "test1" ect. its only showing 1,2,3, or 4. Anyone know why? Thanks a lot.
From the manual -
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
array_rand [uk.php.net] manual page.
So you use the return from array_rand, in your example stored in $get as the key for the answer that you want. So if you change the last line to -
echo $one[$get];
PHP Code: PHP Code: [1][edited by: Simone100 at 7:44 pm (utc) on Jan. 11, 2008]
<?php
setcookie("array", "two", time()+60*60*24*180);
$one = Array();
$one= "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";
?>
<?php
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
echo $two[array_rand($two)];}
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="one"){
setcookie("array", "two", time()+60*60*24*180);
echo $one[array_rand($one)];}
?>
<?php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
echo $two[array_rand($two)];
}else{
setcookie("array", "two", time()+60*60*24*180);
echo $one[array_rand($one)];
}
?>
Buffering is the worst option here, as there is no need to increase server load in this case. There are some cases where it is difficult to reposition code so that headers are not send in the middle of a script. So you use buffering to get around that problem. This is not the case in your script. You may choose to use buffering to compress your pages, but that is a separate thing that we are not talking about.
So I would suggest option 2, as this should be easy to do.
i.e.
<?php
$one = Array();
$one[1] = "test1";
$one[2] = "test2";
$two = Array();
$two[5] = "test5";
$two[6] = "test6";
if(isset($_COOKIE["array"]) && $_COOKIE["array"]=="two"){
setcookie("array", "one", time()+60*60*24*180);
// echo $two[array_rand($two)];
}else{
setcookie("array", "two", time()+60*60*24*180);
//echo $one[array_rand($one)];
}
$cookie = $_COOKIE['array'];
?>
<html>
<head>
</head>
<body>
<?php echo $cookie[array_rand[$cookie]];?>
<p>Did it work?</p>
</body>
</html>