求个PHP数组历遍组合的算法
求个PHP算法$a[1]=array(1,2,3,4,5);$a[2]=array('a','b','c');;$a[3]=array('z','y','x');;要求从...
求个PHP算法
$a[1] = array(1, 2, 3, 4, 5);
$a[2] = array('a', 'b', 'c');;
$a[3] = array('z', 'y', 'x');;
要求从以上N个数组的元素中取一个值,历遍所有可以组合的方式组合成新数组,不重复,结果要求这样的
$b = array(array(1,a,z),array(1,b,z)....array(5,c,x)) 展开
$a[1] = array(1, 2, 3, 4, 5);
$a[2] = array('a', 'b', 'c');;
$a[3] = array('z', 'y', 'x');;
要求从以上N个数组的元素中取一个值,历遍所有可以组合的方式组合成新数组,不重复,结果要求这样的
$b = array(array(1,a,z),array(1,b,z)....array(5,c,x)) 展开
2个回答
展开全部
哥们,跟你个思路,你要学会使用PHP强大的数组处理能力。
思路很简单:把数组全部合并后,用PHP自带的array_unique()把相同元素的值进行移除,你自己试试array_unique()看能不能进行多维数组的元素过滤。
思路很简单:把数组全部合并后,用PHP自带的array_unique()把相同元素的值进行移除,你自己试试array_unique()看能不能进行多维数组的元素过滤。
追问
就是不知道怎么样完成矩阵相乘
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果顺序固定,有算法如下
<?php
$a[] = array(1, 2, 3, 4, 5);
$a[] = array('a', 'b', 'c');
$a[] = array('z', 'y', 'x');
function combine(&$a,$idx){
if($idx>=count($a)-1) return $a[$idx];
else{
$low_b=combine($a,$idx+1);
$b=array();
foreach($a[$idx] as $ch)
foreach($low_b as $low_arr)
$b[]=array_merge(array($ch),is_array($low_arr)? $low_arr: array($low_arr));
return $b;
}
}
$b=combine($a,0);
var_dump($b);
=============
输出
array
0 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'z' (length=1)
1 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'y' (length=1)
2 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'x' (length=1)
3 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'z' (length=1)
4 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'y' (length=1)
5 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'x' (length=1)
6 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'z' (length=1)
7 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'y' (length=1)
8 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'x' (length=1)
9 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'z' (length=1)
10 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'y' (length=1)
11 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'x' (length=1)
12 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'z' (length=1)
13 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'y' (length=1)
14 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'x' (length=1)
15 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'z' (length=1)
16 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'y' (length=1)
17 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'x' (length=1)
18 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'z' (length=1)
19 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'y' (length=1)
20 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'x' (length=1)
21 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'z' (length=1)
22 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'y' (length=1)
23 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'x' (length=1)
24 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'z' (length=1)
25 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'y' (length=1)
26 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'x' (length=1)
27 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'z' (length=1)
28 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'y' (length=1)
29 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'x' (length=1)
30 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'z' (length=1)
31 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'y' (length=1)
32 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'x' (length=1)
33 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'z' (length=1)
34 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'y' (length=1)
35 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'x' (length=1)
36 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'z' (length=1)
37 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'y' (length=1)
38 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'x' (length=1)
39 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'z' (length=1)
40 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'y' (length=1)
41 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'x' (length=1)
42 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'z' (length=1)
43 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'y' (length=1)
44 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'x' (length=1)
====
符合题意
<?php
$a[] = array(1, 2, 3, 4, 5);
$a[] = array('a', 'b', 'c');
$a[] = array('z', 'y', 'x');
function combine(&$a,$idx){
if($idx>=count($a)-1) return $a[$idx];
else{
$low_b=combine($a,$idx+1);
$b=array();
foreach($a[$idx] as $ch)
foreach($low_b as $low_arr)
$b[]=array_merge(array($ch),is_array($low_arr)? $low_arr: array($low_arr));
return $b;
}
}
$b=combine($a,0);
var_dump($b);
=============
输出
array
0 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'z' (length=1)
1 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'y' (length=1)
2 =>
array
0 => int 1
1 => string 'a' (length=1)
2 => string 'x' (length=1)
3 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'z' (length=1)
4 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'y' (length=1)
5 =>
array
0 => int 1
1 => string 'b' (length=1)
2 => string 'x' (length=1)
6 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'z' (length=1)
7 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'y' (length=1)
8 =>
array
0 => int 1
1 => string 'c' (length=1)
2 => string 'x' (length=1)
9 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'z' (length=1)
10 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'y' (length=1)
11 =>
array
0 => int 2
1 => string 'a' (length=1)
2 => string 'x' (length=1)
12 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'z' (length=1)
13 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'y' (length=1)
14 =>
array
0 => int 2
1 => string 'b' (length=1)
2 => string 'x' (length=1)
15 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'z' (length=1)
16 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'y' (length=1)
17 =>
array
0 => int 2
1 => string 'c' (length=1)
2 => string 'x' (length=1)
18 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'z' (length=1)
19 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'y' (length=1)
20 =>
array
0 => int 3
1 => string 'a' (length=1)
2 => string 'x' (length=1)
21 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'z' (length=1)
22 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'y' (length=1)
23 =>
array
0 => int 3
1 => string 'b' (length=1)
2 => string 'x' (length=1)
24 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'z' (length=1)
25 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'y' (length=1)
26 =>
array
0 => int 3
1 => string 'c' (length=1)
2 => string 'x' (length=1)
27 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'z' (length=1)
28 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'y' (length=1)
29 =>
array
0 => int 4
1 => string 'a' (length=1)
2 => string 'x' (length=1)
30 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'z' (length=1)
31 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'y' (length=1)
32 =>
array
0 => int 4
1 => string 'b' (length=1)
2 => string 'x' (length=1)
33 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'z' (length=1)
34 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'y' (length=1)
35 =>
array
0 => int 4
1 => string 'c' (length=1)
2 => string 'x' (length=1)
36 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'z' (length=1)
37 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'y' (length=1)
38 =>
array
0 => int 5
1 => string 'a' (length=1)
2 => string 'x' (length=1)
39 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'z' (length=1)
40 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'y' (length=1)
41 =>
array
0 => int 5
1 => string 'b' (length=1)
2 => string 'x' (length=1)
42 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'z' (length=1)
43 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'y' (length=1)
44 =>
array
0 => int 5
1 => string 'c' (length=1)
2 => string 'x' (length=1)
====
符合题意
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询