84 of 110 menu

PHP Random Array Element

In PHP, a random array element can be output using the array_rand function. This function returns a random key, and not the element itself, but knowing the key we can get the value of the element. See the example:

<?php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]; $key = array_rand($arr); var_dump($key); // random key var_dump($arr[$key]); // random element ?>
English
БеларускаяEspañolРусский
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline