function Combination($sort, $num){ $result = $data = array(); if( $num == 1 ) { return $sort; } foreach( $sort as $k=>$v ) { unset($sort[$k]); $data = Combination($sort,$num-1); foreach($data as $row) { $result[] = $v.','.$row; } } return $result;}$starttime = explode(' ',microtime());$arr = array(1,2,3,4,5,6,7,8,9,1,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);//這個是組合$res = Combination($arr,6);print_r($res);$endtime = explode(' ',microtime());$thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);$thistime = round($thistime,3);echo "本網頁執行耗時:".$thistime." 秒";
我測試了好多,這個效率還可以33選5 1點多秒就出來了,但是選6的話,就出現500錯誤,我的是IIS7.5+PHP5.6 FAST-CGI模式!為啥會500錯誤,是不是數組太大,要調PHP.INI啊。哪個參數是
回複討論(解決方案)
開啟 php 的錯誤顯示功能,你會看到 Fatal error: Allowed memory size of ...... 這樣的錯誤
記憶體不夠了!
考慮是給php分配的記憶體不夠用,ini_set('memory_limit','500M')試下
不知道我理解的對不對
N選M,如果允許重複值,每次調用隨機數鍵名對應的值就好
如果不允許重複值,使用shuffle()函數隨機排列,然後直接取前M個元素