N个数组有M个元素,每个数组必须取一个元素,怎么把他们的全部可能都显示出来
(不知道有没有描述清楚,有点像遍历)例如
数组1(1,2,3,4,5)
数组2 (11,22,33)
数组3 (111,222,333,444,555,666)显示
1,11,111
1,11,222
2,11,111
…………
5,33,666
(不知道有没有描述清楚,有点像遍历)例如
数组1(1,2,3,4,5)
数组2 (11,22,33)
数组3 (111,222,333,444,555,666)显示
1,11,111
1,11,222
2,11,111
…………
5,33,666
$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
$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
)
还想问一个问题调用foo($a, $b, $c) 我不知道数组的数量怎么传值进去?
这个函数是被设计成不定参数的
foo($a, $b)
foo($a, $b, $c)
foo($a, $b, $c, $d)
都可以
$ar = func_get_args();
if(func_num_args() == 1) return call_user_func_array('foo', $ar);//加上这句
$t = array_shift($ar);
if(count($ar) > 1) {
$p = call_user_func_array('foo', $ar);
再麻烦一下
如果我要传入这些数据
$e[] = array(gid =>11,baozhuang => array(2,3,4,5));
$e[] = array(gid =>11,baozhuang => array(11,22,33));
$e[] = array(gid =>11,baozhuang => array(111,222,333,444,555,666));print_R(foo($e));foo函数应该怎么改 谢谢
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);