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!

Recommended Answers

All 3 Replies

Firstly, you could try passing your arrays by reference, rather than duplicating them in memory. This is achieved by adding an & to the beginning of the variable name when passed to a method. E.g. &$arrElement Also, you could simply increase the memory limit allocated to PHP... you can find how to do this here.

Regards,
R.

Firstly, you could try passing your arrays by reference, rather than duplicating them in memory. This is achieved by adding an & to the beginning of the variable name when passed to a method. E.g. &$arrElement Also, you could simply increase the memory limit allocated to PHP... you can find how to do this here.

Regards,
R.

Thanks for the reply!
I have never seen the byRef method in php (Seen it back in good 'ol VB6 :p). Learn something new everyday!

I have gave it a whirl and its still bumming out in the same place. I have root access to the server, but its a live customer hosting server and would rather not go down the mem increase line if possible.

I think the secret will be in optimising the code.

I am having the same error coming up. The code I am using I got it from the php cookbook. The code is fine and optimised. Tried also to increase the memory limit but still the same error. Anyone has worked this out/??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.