воскресенье, 25 марта 2012 г.

array_rand function alternative

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.

 function array_random($arr, $num = 1) {  
   $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, 2));  

1 комментарий:

  1. Рассмешил, содомит.

    Алсо, дропни уже этот пхп, зачем он нужен. Inb4 для лулзов.