[英]PHP array combination with order
我试图弄清楚如何组合两个数组,同时保持PHP中每个单独数组的顺序。
这是一个例子
$ ARRAY1 =阵列( “一”, “B”, “C”, “d”); a必须在b之前,b必须在c之前,c必须在d之前$ array2 = array(“ h”,“ g”,“ f”,“ e”); h必须在g之前,g需要在f之前,f需要在e之前
所有元素都必须存在,因此输出将始终包含所有元素的总和。
最后,我想获得所有组合:
...等等...
我在此站点上找到了很多组合示例,但是没有一个示例可以使输出保持初始顺序。
您是否知道我该怎么做? 我可以反复进行,但是考虑到所有的组合,显然这不是最好的方法。
谢谢洛朗
这是我对脚本的建议:
<?php
$array1 = array("a","b","c","d");
$array2 = array("h","g","f","e");
// produce 8-byte-string with each 4 values of array 1 and 2
// looks like a byte with 8 bits of value 0 and 1
// and delete combinations with other than 4 zeroes and 4 ones
$combos = array();
for($i=0; $i<256; $i++) {
$str = decbin($i);
$str = substr("00000000",0,8-strlen($str)).$str;
// just check if there are 4 ones in the string
if(strlen(str_replace("0","",$str))==4) {
// then add it to combos array
$combos[$str] = array();
// echo it if you like
//echo "<br>".$i.": ".$str;
}
}
# now you have all combinations
# so you can fill it with the array values
foreach($combos as $key => $array) {
$array = array();
$x1=0;
$x2=0;
for($i=0; $i<strlen($key); $i++) {
if(substr($key,$i,1) == "1") {
$array[] = $array1[$x1];
$x1++;
} else {
$array[] = $array2[$x2];
$x2++;
}
}
// echo it if you like
echo "<br>".$key.": ".implode(", ",$array);
$combos[$key] = $array;
}
echo "<br /><br />Combinations: ".count($combos);
?>
这是输出:
00001111: h, g, f, e, a, b, c, d
00010111: h, g, f, a, e, b, c, d
00011011: h, g, f, a, b, e, c, d
00011101: h, g, f, a, b, c, e, d
00011110: h, g, f, a, b, c, d, e
00100111: h, g, a, f, e, b, c, d
00101011: h, g, a, f, b, e, c, d
00101101: h, g, a, f, b, c, e, d
00101110: h, g, a, f, b, c, d, e
00110011: h, g, a, b, f, e, c, d
00110101: h, g, a, b, f, c, e, d
00110110: h, g, a, b, f, c, d, e
00111001: h, g, a, b, c, f, e, d
00111010: h, g, a, b, c, f, d, e
00111100: h, g, a, b, c, d, f, e
01000111: h, a, g, f, e, b, c, d
01001011: h, a, g, f, b, e, c, d
01001101: h, a, g, f, b, c, e, d
01001110: h, a, g, f, b, c, d, e
01010011: h, a, g, b, f, e, c, d
01010101: h, a, g, b, f, c, e, d
01010110: h, a, g, b, f, c, d, e
01011001: h, a, g, b, c, f, e, d
01011010: h, a, g, b, c, f, d, e
01011100: h, a, g, b, c, d, f, e
01100011: h, a, b, g, f, e, c, d
01100101: h, a, b, g, f, c, e, d
01100110: h, a, b, g, f, c, d, e
01101001: h, a, b, g, c, f, e, d
01101010: h, a, b, g, c, f, d, e
01101100: h, a, b, g, c, d, f, e
01110001: h, a, b, c, g, f, e, d
01110010: h, a, b, c, g, f, d, e
01110100: h, a, b, c, g, d, f, e
01111000: h, a, b, c, d, g, f, e
10000111: a, h, g, f, e, b, c, d
10001011: a, h, g, f, b, e, c, d
10001101: a, h, g, f, b, c, e, d
10001110: a, h, g, f, b, c, d, e
10010011: a, h, g, b, f, e, c, d
10010101: a, h, g, b, f, c, e, d
10010110: a, h, g, b, f, c, d, e
10011001: a, h, g, b, c, f, e, d
10011010: a, h, g, b, c, f, d, e
10011100: a, h, g, b, c, d, f, e
10100011: a, h, b, g, f, e, c, d
10100101: a, h, b, g, f, c, e, d
10100110: a, h, b, g, f, c, d, e
10101001: a, h, b, g, c, f, e, d
10101010: a, h, b, g, c, f, d, e
10101100: a, h, b, g, c, d, f, e
10110001: a, h, b, c, g, f, e, d
10110010: a, h, b, c, g, f, d, e
10110100: a, h, b, c, g, d, f, e
10111000: a, h, b, c, d, g, f, e
11000011: a, b, h, g, f, e, c, d
11000101: a, b, h, g, f, c, e, d
11000110: a, b, h, g, f, c, d, e
11001001: a, b, h, g, c, f, e, d
11001010: a, b, h, g, c, f, d, e
11001100: a, b, h, g, c, d, f, e
11010001: a, b, h, c, g, f, e, d
11010010: a, b, h, c, g, f, d, e
11010100: a, b, h, c, g, d, f, e
11011000: a, b, h, c, d, g, f, e
11100001: a, b, c, h, g, f, e, d
11100010: a, b, c, h, g, f, d, e
11100100: a, b, c, h, g, d, f, e
11101000: a, b, c, h, d, g, f, e
11110000: a, b, c, d, h, g, f, e
Combinations: 70
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.