I'm trying to generate an array of all possible data based on 2 arrays:
$arr1 = ['a', 'b', 'c'];
$arr2 = [true, false];
The result should be something like:
[
[
"a" => true,
"b" => true,
"c" => true
],
[
"a" => true,
"b" => false,
"c" => true
],
[
"a" => true,
"b" => false,
"c" => false
],
[
"a" => true,
"b" => true,
"c" => false
],
[
"a" => false,
"b" => true,
"c" => true
]
...
]
This is what I've done so far:
function generateAllCases($arr1, $arr2)
{
$resultArr = [];
foreach ($arr1 as $i => $elm)
{
array_shift($arr1);
foreach ($arr2 as $vis)
{
$resultArr[] =
[
$elm => $vis
];
$resultArr[] = $this->generateAllCases($arr1, $arr2);
}
}
return $resultArr;
}
generateAllCases(['a', 'b', 'c'], [true, false]);
And I'm getting correct results but the array is not formatted as I proposed, I tried different ways to do it, but had no luck to get correct results. I can't get my head around it.
EDIT: if is there a better way to do the loop please let me know.
Any help would be appreciated.