Strange thing happened with array_rand function after PHP 5.2.10 Release... I should paste it from official site:
"The resulting array of keys is no longer shuffled."
C'mon i dont even understand now how it works. Anyway some good man make good and also elegant alternative, when you need to shuffle an array and take some entries from it.
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("apple", "banana", "cherry");
print_r(array_random($a));
print_r(array_random($a, 2));
?>
Рассмешил, содомит.
ОтветитьУдалитьАлсо, дропни уже этот пхп, зачем он нужен. Inb4 для лулзов.