samedi 8 février 2020

How to generate PHP (array_rand) random result as array is given from a variable [duplicate]

I'm trying to understand how does PHP work and got stuck with my code. I knew the code work well when it's written as follow;

$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];

But, when I changed the first line within a variable ($), it won't work. I need to change it due to a loop result form mysql database.

$x = ' "red","green","blue","yellow","brown" ';
$a=array($x);
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];

would anyone explain and give solutions for my above question, very appreciate it. Thanks a lot




Aucun commentaire:

Enregistrer un commentaire