PHP N選M演算法 大數組列印不出來

來源:互聯網
上載者:User
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個元素

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.