mercredi 1 décembre 2021

Exclude elections from array_rand

I found this php code which generates randomdly selects a value between 1 and 9, but excludes those in the array $exclude. IT WORKS.

$exclude = array(1,2,3);
while(in_array(($x = rand(1,9)), $exclude));
echo $x;

Now I want to select one of the letters in the array $items (from 'a' to 'h'), but excluding those in $exclude (from 'a' to 'c'). I use the following code:

$items = array("a", "b", "c", "d", "e", "f", "g", "h");
$exclude = array("a", "b", "c");
$rkey = array_rand($items);
while(in_array(($election = $items[$rkey]), $exclude));
echo $election;

PROBLEM: This works, but after refreshing a number of times, the browser stops working and keeps loading indefinitely. It does not display any error.




Aucun commentaire:

Enregistrer un commentaire