mercredi 6 janvier 2021

php select a random array value within while loop and assign it to a variable

How can I get and assign a random array value from array inside a while loop to a variable?

Code

$stmt = $pdo->prepare("SELECT mem_id FROM members WHERE mem_id NOT IN (SELECT ref_referral FROM referrals)");
$stmt-> execute();

$rec = array();
while($f = $stmt->fetch()){
  $rec[] = $f;
}
print_r($rec);

The above code returns this array

Array
(
    [0] => Array
        (
            [mem_id] => 2
            [0] => 2
        )

    [1] => Array
        (
            [mem_id] => 3
            [0] => 3
        )

    [2] => Array
        (
            [mem_id] => 4
            [0] => 4
        )

)

Now if I want to take one value from either of 2,3,4 randomly and assign it to a variable say $randomValue = $randomValueFromArray, how can I do that?




Aucun commentaire:

Enregistrer un commentaire