Jump to content

Combining items in "n" arrays, "n" being any number


phdphd

Recommended Posts

Hi all,

 

Using a form with check boxes divided into different categories, I collect data selected by the user.

 

I now want to combine each choice in each category with each choice in the other categories and list the combinations to the user.

 

If the user chooses one or more options in each category, it is relatively easy to nest foreach loops, since the number of categories will always be the same. For example, if the form contains three categories of options, the code will be:

 

foreach ($form['cat_1'] as $k1 => $val1)
{
foreach ($form['cat_2'] as $k2 => $val2)
	{
	foreach ($form['cat_3'] as $k3 => $val3)
		{
		echo  $val1.' _ '.$val2.' _ '.$val3.'<br/>';
		}
	}
}

 

Things get complicated if the user does not select options within a given category. More generally, I want to be able to generate the list regardless of the number of categories in which the user selects options.

 

Thank you in advance for your advice.

 

Link to comment
Share on other sites

Combinations, or permutations?

 

<?php

$values = range( 0,rand(5,20) );

echo '<ul>';
// Loop through values
foreach( $values as $key => $value ) {
// Echo out current value within a new list item
echo '<li>'.$value.'-';
// Duplicate array of values before modifying
$subvalues = $values;
// Remove current value from duplicated array of values
unset($subvalues[$key]);
// Implode modified array, and close the list item
echo implode('-',$subvalues).'</li>';
}
echo '</ul>';

?>

 

Is that what you want?

Link to comment
Share on other sites

Well, I am afraid not at first sight :-[

 

Let me give an example with simple data.

 

The data collected in $_POST can be as follows

 

Array
(
    [cat1] => Array
        (
            [0] => house
            [1] => building
        )

    [cat2] => Array
        (
            [0] => cloud
            [1] => sun
        )

    [cat3] => Array
        (
            [0] => happy
            [1] => sad
        )

)

 

The generated list will then be :

 

house _ cloud _ happy

house _ cloud _ sad

house _ sun _ happy

house _ sun _ sad

building_ cloud _ happy

building_ cloud _ sad

building _ sun _ happy

building _ sun _ sad

 

 

Should the user not select any options in cat2, the generated list would be :

 

 

house _ happy

house _ sad

building _ happy

building  _ sad

 

In other words, the PHP code that  generates the list must adapt to whether or not the user did select options in the different categories (whatever the number of categories), because this will determine the number of foreach loops.

 

Thanks in advance !

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

Hi,

Sorry for being late....

The form will look as follows :

<form method="post" action="target.php"><br />
</p>Category1:<br /><br />
<input type="checkbox" name="cat1[]" value="cat_1_val_1">Value 1 of category 1<br/>
<input type="checkbox" name="cat1[]" value="cat_1_val_2">Value 2 of category 1<br/>
<input type="checkbox" name="cat1[]" value="cat_1_val_3">Value 3 of category 1<br/>
<input type="checkbox" name="cat1[]" value="cat_1_val_4">Value 4 of category 1<br/>
<input type="checkbox" name="cat1[]" value="cat_1_val_5">Value 5 of category 1<br/>
<br />
</p>Category2:<br /><br />
<input type="checkbox" name="cat2[]" value="cat_2_val_1">Value 1 of category 2<br/>
<input type="checkbox" name="cat2[]" value="cat_2_val_2">Value 2 of category 2<br/>
<input type="checkbox" name="cat2[]" value="cat_2_val_3">Value 3 of category 2<br/>
<input type="checkbox" name="cat2[]" value="cat_2_val_4">Value 4 of category 2<br/>
<input type="checkbox" name="cat2[]" value="cat_2_val_5">Value 5 of category 2<br/>
<br />
</p>Category3:<br /><br />
<input type="checkbox" name="cat3[]" value="cat_3_val_1">Value 1 of category 3<br/>
<input type="checkbox" name="cat3[]" value="cat_3_val_2">Value 2 of category 3<br/>
<input type="checkbox" name="cat3[]" value="cat_3_val_3">Value 3 of category 3<br/>
<input type="checkbox" name="cat3[]" value="cat_3_val_4">Value 4 of category 3<br/>
<input type="checkbox" name="cat3[]" value="cat_3_val_5">Value 5 of category 3<br/>
<br />
<input type="submit"/> <input type="reset" /></p>
</form>  

 

Thanks for your help.

Link to comment
Share on other sites

If I'm understanding you correctly, you need to write a recursive function, a function that calls itself. Might be it can be solved without though, but I think it would be best solved with a recursive function.

 

Is there always only two values in each category or is there n number of values and categories? n being a natural number, but is it starting from 0 or 1?

 

You want it to write out permutations, but it's not as simple as that. It's like a code lock for a bikecycle, but instead of numbers, here you got various words instead of numbers on each slot.

 

This problem isn't that hard if you think about it a bit abstract!

 

If nobody else does, I will try to write it soon, but a little tip:

Do notice that you mention the first value in the first category x amount of times, where x is the number of values in second category * the number of values in third category * the number of values in fourth category ... etc

Link to comment
Share on other sites

I don't care to do the html for you, but here is what you want:

<?php

function combining_items($categories, $index=0, $string=''){
if(count($categories)==$index){
	echo substr($string, 0, strlen($string)-3).'<br/>';
}else{
	foreach($categories[$index] AS $value){
		combining_items($categories, $index+1, $string.$value.' _ ');
	}
}
}

$categories[] = Array('house', 'building', 'castle');
$categories[] = Array('cloud', 'sun', 'rain');
$categories[] = Array('happy', 'sad', 'excited');

combining_items($categories);

?>

 

output in web browser:

house _ cloud _ happy
house _ cloud _ sad
house _ cloud _ excited
house _ sun _ happy
house _ sun _ sad
house _ sun _ excited
house _ rain _ happy
house _ rain _ sad
house _ rain _ excited
building _ cloud _ happy
building _ cloud _ sad
building _ cloud _ excited
building _ sun _ happy
building _ sun _ sad
building _ sun _ excited
building _ rain _ happy
building _ rain _ sad
building _ rain _ excited
castle _ cloud _ happy
castle _ cloud _ sad
castle _ cloud _ excited
castle _ sun _ happy
castle _ sun _ sad
castle _ sun _ excited
castle _ rain _ happy
castle _ rain _ sad
castle _ rain _ excited

 

It uses the exact same logic as I described in my earlier post! ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.