Home > Backend Development > PHP Tutorial > 经典的笛卡尔乘积代码,要如何修改它

经典的笛卡尔乘积代码,要如何修改它

WBOY
Release: 2016-06-13 13:32:58
Original
1142 people have browsed it

经典的笛卡尔乘积代码,要怎么修改它?
笛卡尔乘积代码:


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;  
}  

$arr = array(  
  array('a1','a2'),  
  'b',  
  array('c1','c2'),  
  array('d1','d2','d3'),
  array('e1','e2','e3')
  //......省略其它元素(也可以是数组),  
);  
   
$r = Descartes( $arr );  
print_r($r);
?>

//////////////////////////////////////

输出:

Array
(
  [0] => Array
  (
  [0] => a1
  [1] => b
  [2] => c1
  [3] => d1
  [4] => e1
  )
  [1] => Array
  (
  [0] => a1
  [1] => b
  [2] => c1
  [3] => d1
  [4] => e2
  )
  ...。。。



////////////////////
现在我只想要3个元素,第一个元素要有a1或a2。那段代码要怎么改?
Array
(
  [0] => Array
  (
  [0] => a1
  [1] => b
  [2] => c1
  )
  ......
  [1] => Array
  (
  [0] => a2
  [1] => c2
  [2] => d3
  )
  ......




------解决方案--------------------
对于
$arr = array(
array('a1','a2'),
'b',
array('c1','c2'),
array('d1','d2','d3'),
array('e1','e2','e3')
//......省略其它元素(也可以是数组),
);
需先对
$arr = array(
'b',
array('c1','c2'),
array('d1','d2','d3'),
array('e1','e2','e3')
//......省略其它元素(也可以是数组),
);
求 M 取 2 的组合
$c = combination($arr, 2);
然后对组合的结果求笛卡尔积

$r = array();
foreach($c as $v)
$r = array_merge($r, Descartes( array('a1','a2'), $v[0], $v[1] )); 
print_r($r);

PHP code
Array
(
    [0] => Array
        (
            [0] => a1
            [1] => d1
            [2] => e1
        )

    [1] => Array
        (
            [0] => a1
            [1] => d1
            [2] => e2
        )

    [2] => Array
        (
            [0] => a1
            [1] => d1
            [2] => e3
        )

    [3] => Array
        (
            [0] => a1
            [1] => d2
            [2] => e1
        )

    [4] => Array
        (
            [0] => a1
            [1] => d2
            [2] => e2
        )

    [5] => Array
        (
            [0] => a1
            [1] => d2
            [2] => e3
        )

    [6] => Array
        (
            [0] => a1
            [1] => d3
            [2] => e1
        )

    [7] => Array
        (
            [0] => a1
            [1] => d3
            [2] => e2
        )

    [8] => Array
        (
            [0] => a1
            [1] => d3
            [2] => e3
        )

    [9] => Array
        (
            [0] => a2
            [1] => d1
            [2] => e1
        )

    [10] => Array
        (
            [0] => a2
            [1] => d1
            [2] => e2
        )

    [11] => Array
        (
            [0] => a2
            [1] => d1
            [2] => e3
        )

    [12] => Array
        (
            [0] => a2
            [1] => d2
            [2] => e1
        )

    [13] => Array
        (
            [0] => a2
            [1] => d2
            [2] => e2
        )

    [14] => Array
        (
            [0] => a2
            [1] => d2
            [2] => e3
        )

    [15] => Array
        (
            [0] => a2
            [1] => d3
            [2] => e1
        )

    [16] => Array
        (
            [0] => a2
            [1] => d3
            [2] => e2
        )

    [17] => Array
        (
            [0] => a2
            [1] => d3
            [2] => e3
        )

    [18] => Array
        (
            [0] => a1
            [1] => c1
            [2] => e1
        )

    [19] => Array
        (
            [0] => a1
            [1] => c1
            [2] => e2
        )

    [20] => Array
        (
            [0] => a1
            [1] => c1
            [2] => e3
        )

    [21] => Array
        (
            [0] => a1
            [1] => c2
            [2] => e1
        )

    [22] => Array
        (
            [0] => a1
            [1] => c2
            [2] => e2
        )

    [23] => Array
        (
            [0] => a1
            [1] => c2
            [2] => e3
        )

    [24] => Array
        (
            [0] => a2
            [1] => c1
            [2] => e1
        )

    [25] => Array
        (
            [0] => a2
            [1] => c1
            [2] => e2
        )

    [26] => Array
        (
            [0] => a2
            [1] => c1
            [2] => e3
        )

    [27] => Array
        (
            [0] => a2
            [1] => c2
            [2] => e1
        )

    [28] => Array
        (
            [0] => a2
            [1] => c2
            [2] => e2
        )

    [29] => Array
        (
            [0] => a2
            [1] => c2
            [2] => e3
        )

    [30] => Array
        (
            [0] => a1
            [1] => c1
            [2] => d1
        )

    [31] => Array
        (
            [0] => a1
            [1] => c1
            [2] => d2
        )

    [32] => Array
        (
            [0] => a1
            [1] => c1
            [2] => d3
        )

    [33] => Array
        (
            [0] => a1
            [1] => c2
            [2] => d1
        )

    [34] => Array
        (
            [0] => a1
            [1] => c2
            [2] => d2
        )

    [35] => Array
        (
            [0] => a1
            [1] => c2
            [2] => d3
        )

    [36] => Array
        (
            [0] => a2
            [1] => c1
            [2] => d1
        )

    [37] => Array
        (
            [0] => a2
            [1] => c1
            [2] => d2
        )

    [38] => Array
        (
            [0] => a2
            [1] => c1
            [2] => d3
        )

    [39] => Array
        (
            [0] => a2
            [1] => c2
            [2] => d1
        )

    [40] => Array
        (
            [0] => a2
            [1] => c2
            [2] => d2
        )

    [41] => Array
        (
            [0] => a2
            [1] => c2
            [2] => d3
        )

    [42] => Array
        (
            [0] => a1
            [1] => b
            [2] => e1
        )

    [43] => Array
        (
            [0] => a1
            [1] => b
            [2] => e2
        )

    [44] => Array
        (
            [0] => a1
            [1] => b
            [2] => e3
        )

    [45] => Array
        (
            [0] => a2
            [1] => b
            [2] => e1
        )

    [46] => Array
        (
            [0] => a2
            [1] => b
            [2] => e2
        )

    [47] => Array
        (
            [0] => a2
            [1] => b
            [2] => e3
        )

    [48] => Array
        (
            [0] => a1
            [1] => b
            [2] => d1
        )

    [49] => Array
        (
            [0] => a1
            [1] => b
            [2] => d2
        )

    [50] => Array
        (
            [0] => a1
            [1] => b
            [2] => d3
        )

    [51] => Array
        (
            [0] => a2
            [1] => b
            [2] => d1
        )

    [52] => Array
        (
            [0] => a2
            [1] => b
            [2] => d2
        )

    [53] => Array
        (
            [0] => a2
            [1] => b
            [2] => d3
        )

    [54] => Array
        (
            [0] => a1
            [1] => b
            [2] => c1
        )

    [55] => Array
        (
            [0] => a1
            [1] => b
            [2] => c2
        )

    [56] => Array
        (
            [0] => a2
            [1] => b
            [2] => c1
        )

    [57] => Array
        (
            [0] => a2
            [1] => b
            [2] => c2
        )

) <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template