hi folks i am have problems with arrays
i have a function that calculates the number of combinations of all elements of an an array.
when i return the array it looks like this.
Array (
[0] => Array (
[0] => 111
)
[1] => Array (
[0] => 201
)
[2] => Array (
[0] => 301
)
[3] => Array (
[0] => 74
)
)
Array (
[0] => Array (
[0] => 111
[1] => 201
)
[1] => Array (
[0] => 111
[1] => 301
)
[2] => Array (
[0] => 111
[1] => 74
)
[3] => Array (
[0] => 201
[1] => 301
)
[4] => Array (
[0] => 201
[1] => 74
)
[5] => Array (
[0] => 301
[1] => 74
)
)
Array (
[0] => Array (
[0] => 111
[1] => 201
[2] => 301
)
[1] => Array (
[0] => 111
[1] => 201
[2] => 74
)
[2] => Array (
[0] => 111
[1] => 301
[2] => 74
)
[3] => Array (
[0] => 201
[1] => 301
[2] => 74
)
)
Array (
[0] => Array (
[0] => 111
[1] => 201
[2] => 301
[3] => 74
)
)
which is right. however i need to be able to store each iteration of my loop into a new array so that i can display doubles trebles 4-timers etc.
I would be grateful for any advice and here is my code😕
<?php
function calculateNumberOfCombinations($newArray, $len)
{
if ($len > count($newArray))
return 'error';
$out = array();
if ($len == 1) {
foreach ($newArray as $v)
$out[] = array($v);
return $out;
}
$len--;
while (count($newArray) > $len) {
$b = array_shift($newArray);
$c = calculateNumberOfCombinations($newArray, $len);
foreach ($c as $v) {
array_unshift($v, $b);
$out[] = $v;
}
}
return $out;
}
$numberOfCombos = array(111, 201,301,74);
for($count=1;$count<=count($numberOfCombos);$count++)
{
$newArray = calculateNumberOfCombinations($numberOfCombos, $count);
print_r($newArray);
}
?>
Again Thanks