21,893
社区成员




class Combination {
var $inputList = array();
var $outputList = array();
public function addArr($arr=array()) {
if (empty($arr) || !is_array($arr)) return false;
array_push($this->inputList, $arr);
}
public function showComninatList(){
if (!empty($this->inputList)) {
if (count($this->inputList) == 1)
return $this->inputList;
foreach ($this->inputList[0] as $key => $value)
$this->outputList = array_merge($this->outputList, $this->recursion(1, $value));
}
}
private function recursion($beforkey, $beforVal){
if (isset($this->inputList[$beforkey])) {
$tempArr = array();
if (!isset($this->inputList[$beforkey+1])) {
foreach ($this->inputList[$beforkey] as $_indexVal)
$tempArr[] = $beforVal.','.$_indexVal;
return $tempArr;
}else {
foreach ($this->inputList[$beforkey] as $value) {
foreach ($this->recursion($beforkey+1, $value) as $hasVal)
$tempArr[] = $beforVal.','.$hasVal;
}
return $tempArr;
}
}
}
}
$a = array('a1','a2','a3');
$b = array('b1','b2','b3');
$c = array('c1','c2','c3');
$com = new Combination();
$com->addArr($a);
$com->addArr($b);
$com->addArr($c);
$com->showComninatList();
print_r($com->outputList);
$a = array(1,2,3,4,5);
$b = array(11,22,33);
$c = array(111,222,333,444,555,666);
print_r(foo($a, $b, $c));
function foo() {
$ar = func_get_args();
$t = array_shift($ar);
if(count($ar) > 1) {
$p = call_user_func_array('foo', $ar);
}else $p = current($ar);
foreach($t as $a) {
foreach($p as $b) {
if(! is_array($a)) $a = array($a);
if(! is_array($b)) $b = array($b);
$r[] = array_merge($a, $b);
}
}
return $r;
}
Array
(
[0] => Array
(
[0] => 1
[1] => 11
[2] => 111
)
[1] => Array
(
[0] => 1
[1] => 11
[2] => 222
)
[2] => Array
(
[0] => 1
[1] => 11
[2] => 333
)
[3] => Array
(
[0] => 1
[1] => 11
[2] => 444
)
$a = array(1,2,3,4,5);
$b = array(11,22,33);
$c = array(111,222,333,444,555,666);
foreach($a as $a1) {
foreach($b as $b1) {
foreach($c as $c1) {
echo "$a1,$b1,$c1<br>";
}
}
}
1,11,111
1,11,222
1,11,333
1,11,444
1,11,555
1,11,666
1,22,111
1,22,222
1,22,333
....
5,33,333
5,33,444
5,33,555
5,33,666