2

I will try to better explain my needs.

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)
);

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.

Thank you for your help

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));
}

?>

I have add str_split for having an array of individual value.

  • think of booleans as bits (cause that's what they are), and then if you have 7 numbers then you have 7 bits ... which means there are 2^7 = 128 combinations. – Jonathan May 02 '15 at 00:18
  • This is why I want to find a way to generate these combinaisons. I don't want to write 128 line of code. Because after, I will need to test on a 30 value array... – Benjamin Dubé May 02 '15 at 00:21
  • 1
    possible duplicate of [Finding the subsets of an array in PHP](http://stackoverflow.com/questions/6092781/finding-the-subsets-of-an-array-in-php) –  May 02 '15 at 00:23
  • @Dagon, it is definitely a duplicate. – Rohit Gupta May 02 '15 at 00:25
  • I've read this question, I don't think it can help me alot, I need to create with seven boolean value from scratch. I will rewrite my question, I surely did wrong with my english... – Benjamin Dubé May 02 '15 at 00:28
  • If you try to keep all permutations of 30 booleans in an array, assuming that a boolean takes up one bit, you will need at least 30 gigabytes of memory. A boolean probably takes up more memory and then there is the overhead of the arrays. Rethink your approach. Why do you need 2^30 options? There is nothing you can do with a list of 1 billion items. Your problem sounds like an xy-problem http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem don't tell us about y, tell us about x. – Halcyon May 02 '15 at 00:31
  • I don't know about xy problem before, now I know! I need all this array for testing an algorithm. The algoryth give a score to an user who have on a 7 days, sucessfully(true) or not(false) done something. This is why, I want to test all possibility. The 128 array will simulate all possibility. I can juste use the number possible of success and failure, because I need to check the consecutive fail and success – Benjamin Dubé May 02 '15 at 00:57

2 Answers2

3

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;
}
Jonathan
  • 2,778
  • 13
  • 23
  • Thanks, I don't know why your answer was downvote, with a minor change, it does wexactly what I want ! I will post my modification for render 0 and 1 in an array. – Benjamin Dubé May 02 '15 at 00:51
  • glad it worked for you, just don't change that to 30, as it will have 1,073,741,824 combinations and will surely crash your computer :) – Jonathan May 02 '15 at 01:00
  • I've test, with 30 I got an PHP memory, I've test the limit, and I successfully go to 15 :) Thanks – Benjamin Dubé May 02 '15 at 01:10
0

Here is a solution based on bitwise operators and short syntax:

$length = 7;
$totalCombos = 2 ** $length;
$sequences = [];

for($i = 0; $i < $totalCombos; $i++) {
    $row = [];

    for ($j = 0; $j < $length; $j++) {
        $row[] = (bool)(($i >> $length) & 1);
    }
    
    $sequences[] = $row;
}
Oleksandr Savchenko
  • 642
  • 3
  • 10
  • 35