0

Hi All,

I have coded a function to work out all the possible combinations givien a list of properties and options. If the script was working out all the possible ways people would like toast, it might look like this.

```
Array
(
[Bread] => Array
(
[0] => White
[1] => Wholemeal
)
[Colour] => Array
(
[0] => Lightly Toasted
[1] => Well Toasted
[2] => Burnt
)
[Butter] => Array
(
[0] => Yes
[1] => No
)
[Jam] => Array
(
[0] => None
[1] => Strawberry
[2] => Raspberry
[3] => Lemon & Lime
)
)
```

The Function is

```
function combinations($elements) {
if(is_array($elements)){
$combinations=array(array());
foreach($elements as $key=>$value){
foreach ($value as $k=>$element){
$new_combinations=array();
foreach ($combinations as $combination) {
$tmp = array($key=>$k);
$new_combination=array_merge($combination,$tmp);
unset($tmp,$combination);
array_push($new_combinations,$new_combination);
unset($new_combination);
}
$combinations=array_merge($combinations,$new_combinations);
}
}
for($j=0;$j<count($elements);$j++){
for($i=0;$i<count($combinations);$i++){
if((count($combinations[$i]))<(count($elements))){
unset($combinations[$i]);
}
}
$combinations = array_values($combinations);
}
return array_values(array_unique($combinations));
}
else{
return false;
}
}
```

If more than roughyly 6 options with 3 properties each are given, the function fails giving a memory limit error on either array_merge lines.

Do any of you clever people have any suggestions on how to improve this function and make it more efficent?

TIA for the advice!