This question already has an answer here:
这个问题在这里已有答案:
- How to generate in PHP all combinations of items in multiple arrays 5 answers
如何在PHP中生成多个数组中所有项目的组合5个答案
I apologize first, but I've been coding for about 8 hours today to get this last thing done.
我先道歉,但是我今天已经编写了大约8个小时来完成最后一件事。
Code.
$a = array('a', 'c', 'b');
$c = array('a', 'c', 'b');
foreach(array_combine($a, $c) as $k => $v) {
echo $k.$v;
}
resulting in: aa, bb, cc
结果是:aa,bb,cc
but I want to get@
但我想得到@
aa, ac, ab
ba, bc, bb
ca, cc, cb
2 个解决方案
#1
0
I recommend you to use foreach function. Don't use array_combine().
我建议你使用foreach功能。不要使用array_combine()。
function cloop($a, $c) {
$a = array('a', 'b', 'c');
$c = array('d', 'e', 'f');
foreach($a as $A){
foreach($c as $B) {
$ab = $A.$B;
}
}
return $ab;
}
#2
4
If you want all permutations, then I'm not sure you want to use array_combine(). Just use nested loops, like this:
如果你想要所有的排列,那么我不确定你是否想要使用array_combine()。只需使用嵌套循环,如下所示:
$a = array('a', 'c', 'b');
$c = array('a', 'c', 'b');
foreach($a as $v1){
foreach($c as $v2) {
echo $v1.$v2;
}
}
#1
0
I recommend you to use foreach function. Don't use array_combine().
我建议你使用foreach功能。不要使用array_combine()。
function cloop($a, $c) {
$a = array('a', 'b', 'c');
$c = array('d', 'e', 'f');
foreach($a as $A){
foreach($c as $B) {
$ab = $A.$B;
}
}
return $ab;
}
#2
4
If you want all permutations, then I'm not sure you want to use array_combine(). Just use nested loops, like this:
如果你想要所有的排列,那么我不确定你是否想要使用array_combine()。只需使用嵌套循环,如下所示:
$a = array('a', 'c', 'b');
$c = array('a', 'c', 'b');
foreach($a as $v1){
foreach($c as $v2) {
echo $v1.$v2;
}
}