shlomikalfa Posted February 5, 2010 Share Posted February 5, 2010 Hi, I've this topic which deals with something similar but not exactly the same... As my question is a bit more difficult... I'd like to know how to get all the possible answer results of a an online survey. Ex: Question1 - 1/2/3 Question2 - 1/2 Question3 - 1/2 All possible permutations of the above survey will be 3*2*2 (12) that i know... What I need is to output all the possible results into a table... Ex: __________________ 1,1,1 | 2,1,1 | 3,1,1 | 1,1,2 | 2,1,2 | 3,1,2 | 1,2,1 | 2,2,1 | 3,2,1 | 1,2,2 | 2,2,2 | 3,2,2 | __________________| I know i should use some recursive method for that... any idea ?! Any help will be appreciated. Thanks in advance, SK. Link to comment https://forums.phpfreaks.com/topic/191050-mathstatistics-all-possible-combinations-of-a-surveys-answers/ Share on other sites More sharing options...
shlomikalfa Posted February 5, 2010 Author Share Posted February 5, 2010 no one ?! Link to comment https://forums.phpfreaks.com/topic/191050-mathstatistics-all-possible-combinations-of-a-surveys-answers/#findComment-1007500 Share on other sites More sharing options...
ignace Posted February 5, 2010 Share Posted February 5, 2010 post some more information regarding your database structure Link to comment https://forums.phpfreaks.com/topic/191050-mathstatistics-all-possible-combinations-of-a-surveys-answers/#findComment-1007515 Share on other sites More sharing options...
shlomikalfa Posted February 5, 2010 Author Share Posted February 5, 2010 Well... there isn't much more to it... I have an obj with question in it: $all_question Inside, it has the questions... each of them has several number of answers... (different count). I want to be able to output all the possible combinations of $all_question. I can't explain it any better than I already did i think ... This is what i have so far: $rules = 1; // At least 1 rule. // First, calculate how many rules do we need to create. foreach($all_question as $question) { $rules *= $question->answer_count; } // Parse all rules. $ruleArr = array(); $ruleArrStr = array(); $lastQ = count($all_question)-1; $lastC = $lastQ; for ($i = 0; $i < $rules; $i++){ // Set the array to work with. for ($c = 0; $c < count($all_question); $c++){ $ruleArr[$i][$c] = 1; } $qn = 0; $str = implode(', ', $ruleArr[$i]); while(array_search($str, $ruleArrStr)!==false){ while($ruleArr[$i][$lastC] == $all_question[$lastC]->answer_count){ $lastC-=1; if($lastC==0){break 2;} } $ruleArr[$i][$lastC] += 1; $lastC = $lastQ; $str = implode(', ', $ruleArr[$i]); echo 'search: '.$str; } $ruleArrStr[$i] = implode(', ', $ruleArr[$i]); } echo "<br /><br />"; print_r($ruleArrStr); It's not working ofc.... I'm trying to move backwards... like: 1,1,1,1 1,1,1,2 1,1,1,3 1,1,2,1 1,1,2,2 1,1,2,3 ........ Link to comment https://forums.phpfreaks.com/topic/191050-mathstatistics-all-possible-combinations-of-a-surveys-answers/#findComment-1007522 Share on other sites More sharing options...
kathas Posted February 10, 2010 Share Posted February 10, 2010 $n = count($all_question)-1; $answers = array(); $answer = array_fill(0,$n,1); $change = $n; while ($change > 0 || $answer[$change]<$all_question[$change]->answer_count) { if ($answer[$change]<$all_question[$change]->answer_count) { $answer[$change]++; $answers[] = implode($answer,','); $change = $n; } else { $answer[$change]=1; $change--; } } I tested it with this output. The first question has 3 answers second has only 1 third 2 4th 3 Array ( [0] => 1,1,1,1 [1] => 1,1,1,2 [2] => 1,1,1,3 [3] => 1,1,2,1 [4] => 1,1,2,2 [5] => 1,1,2,3 [6] => 2,1,1,1 [7] => 2,1,1,2 [8] => 2,1,1,3 [9] => 2,1,2,1 [10] => 2,1,2,2 [11] => 2,1,2,3 [12] => 3,1,1,1 [13] => 3,1,1,2 [14] => 3,1,1,3 [15] => 3,1,2,1 [16] => 3,1,2,2 [17] => 3,1,2,3 ) Link to comment https://forums.phpfreaks.com/topic/191050-mathstatistics-all-possible-combinations-of-a-surveys-answers/#findComment-1010286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.