求數組組合演算法解決辦法

來源:互聯網
上載者:User
求數組組合演算法
PHP code
array(2) {  [8] => array(2) {    [0] => string(2) "63"    [1] => string(2) "64"  }  [9] => array(2) {    [0] => string(2) "78"    [1] => string(2) "79"  }}


2個數組之間的組合 多個數組呢?求高手啊

------解決方案--------------------
PhpNewnew版主討論過這個,傳送門

http://topic.csdn.net/u/20120325/11/cb8beb24-845c-4d16-be52-92f74b21a30c.html
------解決方案--------------------
這是個求笛卡爾積的問題
PHP code
$ar = array(  8 => array('63', '64'),  9 => array('78', '79'),);print_r(Descartes($ar));function Descartes() {  $t = func_get_args();  if(func_num_args() == 1) return call_user_func_array( __FUNCTION__, $t[0] );   $a = array_shift($t);  if(! is_array($a)) $a = array($a);  $a = array_chunk($a, 1);  do {    $r = array();    $b = array_shift($t);    if(! is_array($b)) $b = array($b);    foreach($a as $p)        foreach(array_chunk($b, 1) as $q)            $r[] = array_merge($p, $q);    $a = $r;  }while($t);  return $r;} 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.