This question already has an answer here:
we have 3 school branches:
xschool atlanta
xschool ortigas
xschool bagio
I will generate a simple combinations.
How can I generate the array which is below in php language
arr1[0]=> xschool atlanta
arr1[1]=>xschool ortigas
arr1[2]=>xschool bagio
arr1[3]=>xschool atlanta,xschool ortigas
arr1[4]=>xschool atlanta,xschool bagio
arr1[5]=>xschool ortigas,xschool bagio
arr1[6]=>xschool atlanta,xschool ortigas,xschool bagio
I wasn't able to set up algorithm in my mind...
Why I need this algorithm? Sometimes our parttime teachers can work in 2 branches different day. So when we add the teacher to our system, the combobox should show us the list(array) which is above.
</div>
Yes, I solved my problem. Here the code:
function combine_set_well($array) {
// initialize by adding the empty set
$results = array(array( ));
foreach ($array as $element)
foreach ($results as $combination)
array_push($results, array_merge(array($element), $combination));
return $results;
}
$set = array('xschool atlanta', 'xschool ortegas', 'xschool bagio');
$ayarla=combine_set_well($set);
asort($ayarla);
foreach ($ayarla as $combination) {
print join(",", $combination) . "<br/>";
}
I might provide you a direction (pseudocode), this is just one of many ways to solve this. You got to implement your own code ;).
Generate a list of binary number from 000 -> 111 Each number match to 1 permutation.
000
001 => xschool atlanta
010 => xschool ortigas
100 => xschool bagio
110 => xschool atlanta,xschool ortigas
101 => xschool atlanta,xschool bagio
011 => xschool ortigas,xschool bagio
111 => xschool atlanta,xschool ortigas,xschool bagio
The rest you got to do by yourself. Have fun coding ;)