Page 1 of 1
Array Problem - Trying To Find All Combinations
Posted: Sun Jun 28, 2009 11:47 pm
by jbh
Hey,
While I have seen demos online, they do not fit what I am looking for. Imagine having 2 arrays:
Array ( [gender] => male,female)
And
Array ( [age] =>18-25,26-35,36-45,46-65,65+, )
I want to basically create as many combos between the 2 as I can (total is 10). Such as
Male - 18-25
Male - 26-35 ....
Male 65+
Female 18-25
....
Female 65+
Truth is, I am pulling questions from a db and each q has answers. I want to loop through all questions
and then the answers (The above demo shows answers for question "What is your gender" and then "What is your age"
and create an array list that follows the rules above but I cannot find a way to take the data,
that I am looping through, and get it to create this permutation list.
Every example I see doesn't seem to fit what I want. I just cannot work with arrays well enough to visualize
where to start.
Just wondering if anybody had experience with something like this.
Thank you very much for your time. I appreciate it.
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 12:42 am
by requinix
Use nested loops. The one on the outside has whatever changes less often (that's the gender), the one on the inside has whatever changes most often (that's the age).
Code: Select all
$outer = array("Alice", "Bob", "Cindy");
$inner = array("Milton", "Newton", "O'Reilly", "Pratchett");
foreach ($outer as $firstname) {
foreach ($inner as $lastname) {
echo $firstname, " ", $lastname, "<br>\n";
}
}
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 7:02 am
by prometheuzz
jbh wrote:...
Just wondering if anybody had experience with something like this.
...
If your arrays will always be just two of them, use the suggestion from the previous poster. However, if you have an arbitrary number of arrays, generating loops on the fly will not work AFAIK (but I know very little PHP, so that might be untrue). What I
do know something about is algorithmic-trickery, so here's my suggestion of generating all combinations with an arbitrary number of arrays:
Code: Select all
function combinations($sets) {
$solutions = 1; // total solution equals 2*5*3 = 30
foreach($sets as $set) {
$solutions *= sizeof($set);
}
for($i = 0; $i < $solutions; $i++) {
$j = 1;
foreach($sets as $set) {
echo "{$set[($i/$j)%sizeof($set)]}\t";
$j *= sizeof($set);
}
echo "\n";
}
}
combinations(
array(
array('male', 'female'),
array('18-25', '26-35', '36-45', '46-65', '65+'),
array('smoking', 'drinking', 'health-freak')
)
);
/* output:
male 18-25 smoking
female 18-25 smoking
male 26-35 smoking
female 26-35 smoking
male 36-45 smoking
female 36-45 smoking
male 46-65 smoking
female 46-65 smoking
male 65+ smoking
female 65+ smoking
male 18-25 drinking
female 18-25 drinking
male 26-35 drinking
female 26-35 drinking
male 36-45 drinking
female 36-45 drinking
male 46-65 drinking
female 46-65 drinking
male 65+ drinking
female 65+ drinking
male 18-25 health-freak
female 18-25 health-freak
male 26-35 health-freak
female 26-35 health-freak
male 36-45 health-freak
female 36-45 health-freak
male 46-65 health-freak
female 46-65 health-freak
male 65+ health-freak
female 65+ health-freak
*/
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 9:03 am
by jbh
Thank you for the replies. For the record, the # of arrays will be dynamic. For instance, what I am really doing is pulling a query that says
"FInd me every question where it's 'type' is segmenting (as in, a category question, not a 'yes/no' or 'correct answer' question)
I could have 3, 2, 1 or 5 at any point. If I happen to have 3:
age, gender, income then we might find that they each have the following answers attached to the question:
Age ==> 18-25, 26-35....65+
Income ==> high, low
Gender ==> male, female
So, during this query I am pulling these values, along with the 3 questions, and trying to create an associative array (I guess that's the best idea) and then trying to find a way to display all permutations.
I will try the solutions and see if they fit. I just wanted to be clear on why I need to do it and how I am doing it. I am successfully *pulling* and displaying the records, just not successfully creating the array and displaying all possibilities.
Thank you for your time, again.
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 9:09 am
by jbh
Hard coding the array, as in the *2nd reply example*, works like a charm. So I just have to
create that multi-array dynamically. My test output for now, when I pull the appropriate q and corresponding answers from the db, is:
What Gender Are You?
--> male
--> female
What Age Are You?
--> 18-25
--> 26-35
--> 36-45
--> 46-65
--> 65+
So, I will dynamically build an array out of it like this, I guess:
array(
array('male', 'female'),
array('18-25', '26-35', '36-45', '46-65', '65+'));
Question is if I can turn it into a string that simulates the structure of above and then use that var in the combination function
or if I must build it another way? I will test it out but I figured I'd ask as I have not done anything like *that* before (never had to generate dynamic arrays. This is interesting)
Thanks
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 9:45 am
by prometheuzz
Well, like I said, I have little knowledge of PHP. So, unfortunately, I can't be of any more help than providing the algorithm to generate the combinations.
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 10:01 am
by jbh
NP. Your function is brilliant and will work when I can dynamically create that array
Re: Array Problem - Trying To Find All Permutations
Posted: Mon Jun 29, 2009 10:52 am
by prometheuzz
jbh wrote:NP. Your function is brilliant and will work when I can dynamically create that array
Glad to hear that.
Just one little nit-pick from my side: in your title you mentioned permutations, but, just for the record, you're looking for combinations.
Best of luck with your problem!
Re: Array Problem - Trying To Find All Combinations
Posted: Mon Jun 29, 2009 1:13 pm
by jbh
May I ask how I can format it so I can display it in a neat format such as
Male | 18-25 | smoker via html while including a hidden field? Such as
<input type="hidden" name="combo" value="male_18-25_smoker">
The idea is I want it to be more readable to a visitor while I also track the values in a hidden form
because I will be appending a text field next to each printed row.
I am trying to futz with the
echo "{$set[($i/$j)%sizeof($set)]}\t";
but I cannot seem to get it to do that.
Either way, i will keep working on it, In the meantime and I really want to thank you for this.
Re: Array Problem - Trying To Find All Combinations
Posted: Mon Jun 29, 2009 1:36 pm
by prometheuzz
Perhaps this is less cryptic:
Code: Select all
function combinations($sets) {
$combs = array(); // create an array to hold all combinations
$solutions = 1; // total solution equals 2*5*3 = 30
foreach($sets as $set) {
$solutions *= sizeof($set); // calculate the total number of solutions
}
for($i = 0; $i < $solutions; $i++) {
$j = 1;
foreach($sets as $set) {
$combs[$i] .= $set[($i/$j)%sizeof($set)] . "_"; // append another element and an underscore
$j *= sizeof($set);
}
$combs[$i] = preg_replace('/_$/', '', $combs[$i]); // remove the last underscore
}
return $combs; // return all combinations
}
$combs = combinations(
array(
array('male', 'female'),
array('18-25', '26-35', '36-45', '46-65', '65+'),
array('smoking', 'drinking', 'health-freak')
)
);
print_r($combs);
/* output
Array
(
[0] => male_18-25_smoking
[1] => female_18-25_smoking
[2] => male_26-35_smoking
[3] => female_26-35_smoking
[4] => male_36-45_smoking
[5] => female_36-45_smoking
[6] => male_46-65_smoking
[7] => female_46-65_smoking
[8] => male_65+_smoking
[9] => female_65+_smoking
[10] => male_18-25_drinking
[11] => female_18-25_drinking
[12] => male_26-35_drinking
[13] => female_26-35_drinking
[14] => male_36-45_drinking
[15] => female_36-45_drinking
[16] => male_46-65_drinking
[17] => female_46-65_drinking
[18] => male_65+_drinking
[19] => female_65+_drinking
[20] => male_18-25_health-freak
[21] => female_18-25_health-freak
[22] => male_26-35_health-freak
[23] => female_26-35_health-freak
[24] => male_36-45_health-freak
[25] => female_36-45_health-freak
[26] => male_46-65_health-freak
[27] => female_46-65_health-freak
[28] => male_65+_health-freak
[29] => female_65+_health-freak
)
*/
Re: Array Problem - Trying To Find All Combinations
Posted: Mon Jun 29, 2009 1:53 pm
by jbh
Ok, that makes sense. PM me if you have a paypal account and first chance I want to send you a donation
Or at least send a wish list
TY for the time and for making it dummy friendly.

Re: Array Problem - Trying To Find All Combinations
Posted: Mon Jun 29, 2009 2:03 pm
by prometheuzz
jbh wrote:Ok, that makes sense. PM me if you have a paypal account and first chance I want to send you a donation
Or at least send a wish list
TY for the time and for making it dummy friendly.

It really was no problem. Your appreciation is more than enough "payment" for me: there's no need for
pay-palling me (besides, I don't even have such an account!).
Best of luck!