string(2) "64" } [9] => array(2) { [0] => string("/> string(2) "64" } [9] => array(2) { [0] => string(">

Home >Backend Development >PHP Tutorial > 求数组组合算法解决办法

求数组组合算法解决办法

WBOY
WBOYOriginal
2016-06-13 13:35:26932browse

求数组组合算法

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

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;
} <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn