Basically, what I did is I took this answer's code to find out all the possible "number is there" / "number is not there" combinations, like the following:
0 0 0 0
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
0 1 0 0
and so on...
Afterwards I applied this to the input
arrays contents.
<?php
//First of all, find out all possible combinations of "number is there" and "number is not there"
$input = ["1","2","4","5"];
$empty = " "; //Value to insert if "number is not there"
$length = sizeof($input); //How many numbers are in the array
$combinations = pow(2, $length); //Number of possible 1/0 combinations
$sequence = array();
for($x = 0; $x < $combinations; $x++) {
$sequence[$x] = str_split(str_pad(decbin($x), $length, 0, STR_PAD_LEFT));
}
//Alright, so now we have to apply these combinations to our $input array
$output = [];
foreach($sequence as $combo){
$output_combo = [];
foreach($combo as $key=>$val){
if ($val == 1){
//"number is there"
array_push($output_combo, $input[$key]);
}else{
//"number is not there"
array_push($output_combo, $empty);
}
}
array_push($output, $output_combo);
}
//There you go! The variable $output contains your array.
//Display the array (You can leave this part out...)
foreach($output as $combo){
foreach($combo as $val){
echo "[" . $val . "] ";
}
echo "<br>";
}
?>
Check out this PHPFiddle - it works with as many numbers as you want. Just make sure the $empty
variable contains the value that you want to have if the number is not there.
Simply click this link and choose "Run": http://phpfiddle.org/main/code/fdtn-u5jv