所有可能的组合中列出PHP中的布尔值的PHP数组
我会尝试更好地解释我的需求。
I will try to better explain my needs.
我需要从scrach,谁包含许多阵列的多向阵列产生。内部数组必须包含7布尔值数组。我需要所有的combinaisons可能的7布尔值。随着7例,它使128内的阵列。
I need to generate from scrach, a multidirectional array who contain many array. The inner array must be array containing 7 boolean value. I need to have all the combinaisons possible of 7 boolean values. With 7 cases, it make 128 inner arrays.
下面输出的例子,我需要,对于因此很容易理解:
Here an example of output I need, for making it easy to understand :
$sequence = array(
array(true, true, true, true, true, true, true),
array(true, true, true, true, true, true, false),
array(true, true, true, true, true, false, false)
);
我需要所有combinaison可能为每7阵列值布尔值的。
I need to have all combinaison possible of Boolean value for each 7 array value.
我尝试这个发布之前找到解决方案,但我什么也没找到。
I try to find solution before posting this, but I found nothing.
感谢您的帮助。
对-S-:后来,我需要做同样的事情,但每个表,而不是7.30布尔值因此,如果该解决方案可以灵活,它的奖金!
P-S.: Later, I will need to do the same thing, but with 30 boolean values per table instead of 7. So if the solution can be flexible, it's a bonus!
解决方案:
这是我如何顺利拿到我需要什么,我跟检查解决方案稍加修改。
Solution : This is how I've successfully got what I need, with a little modification of the solution I checked.
<?php
$length = 7;
$totalCombos = pow(2, $length);
$sequences = array();
for($x = 0; $x < $totalCombos; $x++) {
$sequence[$x] = str_split(str_pad(decbin($x), $length, 0, STR_PAD_LEFT));
}
?>
我有加str_split为具有独立价值的数组。
I have add str_split for having an array of individual value.
这将做到这一点,它是灵活的。
This will do it and it's flexible.
<?php
$length = 7;
$totalCombos = pow(2, $length);
for($x = 0; $x < $totalCombos; $x++) {
echo str_pad(decbin($x), $length, 0, STR_PAD_LEFT) . PHP_EOL;
}