永发信息网

php若干数组组合算法

答案:2  悬赏:30  手机版
解决时间 2021-02-26 00:50
  • 提问者网友:送舟行
  • 2021-02-25 00:56
$a=Array ( [0] =>黑色 [1] => 白色 )
$b=Array ( [0] =>36码 [1] => 37码 )
$c=Array ( [0] =>男款 [1] => 女款 )

求用php组合成
$d=Array ( [0] =>黑色36码男款 [1] =>黑色36码女款 [2] =>黑色37码男款 [3] =>黑色37码女款 [4] =>白色36码男款 [5] =>白色36码女款 [6] =>白色37码男款 [7] =>白色37码女款)

$a=Array ('黑色',' 白色' )
$b=Array ('36码','37码' )
$c=Array ( '男款',' 女款' )
求用php组合成
$d=Array ('黑色36码男款','黑色36码女款','黑色37码男款','黑色37码女款','白色36码男款','白色36码女款','白色37码男款','白色37码女款')
最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-02-25 01:50
$a=array ( '黑色','白色' );
$b=array ( '36码' ,'37码' );
$c=array ( '男款' ,'女款' );

$d = array();
foreach ($a as $i => $_a ){
foreach ($b as $ii => $_b ){
foreach ($c as $iii => $_c ){
$d[] = $_a.$_b.$_c;
}
}
}
print_r($d);

Array
(
[0] => 黑色36码男款
[1] => 黑色36码女款
[2] => 黑色37码男款
[3] => 黑色37码女款
[4] => 白色36码男款
[5] => 白色36码女款
[6] => 白色37码男款
[7] => 白色37码女款
)
全部回答
  • 1楼网友:蕴藏春秋
  • 2021-02-25 03:19
如果顺序固定,有算法如下 =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) ==== 符合题意
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯