# 请教数字拆分组合的问题

aoonet 经理  2017-11-17 05:23:45

...全文
137 3 点赞 打赏 收藏 举报

3 条回复

xuzuning 2017-11-17
``````foreach(Descartes(str_split(123), str_split(456)) as \$v)
echo join('', \$v), PHP_EOL;

function Descartes(\$d) {
if(func_num_args() > 1) \$d = func_get_args();
\$r = array_pop(\$d);
while(\$d) {
\$t = array();
\$s = array_pop(\$d);
if(! is_array(\$s)) \$s = array(\$s);
foreach(\$s as \$x) {
foreach(\$r as \$y) \$t[] = array_merge(array(\$x), is_array(\$y) ? \$y : array(\$y));
}
\$r = \$t;
}
return \$r;
}``````
``````
14
15
16
24
25
26
34
35
36``````
• 打赏
• 举报

``````
\$a1 = str_split(123);
\$a2 = str_split(456);
\$new = test(\$a1,\$a2);
echo "<pre>";
print_r(\$new);
echo "</pre>";
function test(\$arr1,\$arr2,\$index1=0,\$index2=0) {
static \$result = array();
\$result[] = \$arr1[\$index1].\$arr2[\$index2];

if((\$index1==count(\$arr1)-1) && (\$index2==count(\$arr2)-1)){
return \$result;
}else{
if(\$index2==count(\$arr2)-1){
\$index1++;
\$index2 = 0;
}else{
\$index2++;
}
return test(\$arr1,\$arr2,\$index1,\$index2);
}
}
/*
Array
(
[0] => 14
[1] => 15
[2] => 16
[3] => 24
[4] => 25
[5] => 26
[6] => 34
[7] => 35
[8] => 36
)
*/
``````
• 打赏
• 举报

``````\$s1 = 123;
\$s2 = 456;
foreach(str_split(\$s1) as \$v1){
foreach(str_split(\$s2) as \$v2){
\$arr[] = \$v1.\$v2;
}
}
print_r(\$arr);``````
Array ( [0] => 14 [1] => 15 [2] => 16 [3] => 24 [4] => 25 [5] => 26 [6] => 34 [7] => 35 [8] => 36 )
• 打赏
• 举报

2.1w+

2017-11-17 05:23