mysql - zend framework and php, increment only if set of values in both arrays are equal -


i have 2 arrays. first array contains result of database query. database of information. second array contains user defined values selected on screen. goal reconcile elements user has clicked database result set. end result trying achieve following "rule": if group of elements match group each array, id increment counter. not key values need match, ones. example if db result array contains 3 values, , user array contains 2 matching values not increment. if db result array contains 3 values, , user array contains 3 matching values increment.

an example of each multidimensional array provided:

//db results - $result() array (      [0] => array (        [id] => 115        [q_id] => question-1        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-1-drop-down-answer-1-input        [a_body] => august [answer] => yes        [timestamp] => 2014-11-30 21:36:02     )  [1] => array (        [id] => 117        [q_id] => question-1        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-1-drop-down-answer-3-input [a_body] => 10        [answer] => yes [timestamp] => 2014-11-30 21:36:02  )  [2] => array (        [id] => 119        [q_id] => question-1        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-1-drop-down-answer-5-input        [a_body] => 2013 [answer] => yes [timestamp] => 2014-11-30 21:36:02  )  [3] => array (        [id] => 136        [q_id] => question-2        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-2-drop-down-answer-1-input        [a_body] => december [answer] => yes        [timestamp] => 2014-11-30 21:36:02  )  [4] => array (        [id] => 139        [q_id] => question-2        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-2-drop-down-answer-4-input        [a_body] => 23        [answer] => yes        [timestamp] => 2014-11-30 21:36:02  )  [5] => array (        [id] => 141        [q_id] => question-2        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-2-drop-down-answer-6-input        [a_body] => 1984        [answer] => yes        [timestamp] => 2014-11-30 21:36:02  )  [6] => array (        [id] => 149 [q_id] => question-3        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-3-drop-down-answer-1-input        [a_body] => febraury        [answer] => yes        [timestamp] => 2014-11-30 21:36:02  )  [7] => array (        [id] => 150        [q_id] => question-3        [course_id] => 1        [course_section] => unit-2-section-2        [a_id] => question-3-drop-down-answer-2-input        [a_body] => 14        [answer] => yes        [timestamp] => 2014-11-30 21:36:02        )  )  //user input array - $dataanswers() array (          [0] => array (                  [questionid] => question-1                  [courseid] => 1                  [coursesection] => unit-2-section-2                  [questiontype] => select-all                  [selectedanswer] => august          )           [1] => array (                  [questionid] => question-2                  [courseid] => 1                  [coursesection] => unit-2-section-2                  [questiontype] => select-all                  [selectedanswer] => december          )          [2] => array (                  [questionid] => question-3                  [courseid] => 1                  [coursesection] => unit-2-section-2                  [questiontype] => select-all                  [selectedanswer] => febraury          )          [3] => array (                  [questionid] => question-3                  [courseid] => 1                  [coursesection] => unit-2-section-2                  [questiontype] => select-all                  [selectedanswer] => 14          )  ) 

here first attempt cant seem wrap head around logically. suggestion appreciated:

//counters     $i = 0;      $f = 0;       foreach ($dataanswers $a){          $e = 0;          foreach ($result $r){              if ($a['questionid'] == $r['q_id']) {                 $e++;             }              if ($a['questionid'] == $r['q_id'] && $a['selectedanswers'] == $r['a_body']) {                 $f++;             }              if($e == $f){                 $i++;             }          }       } 

i should conclude above example expected provide value of $i = 1.

the idea create 2 arrays , count how many elements same key have same value between 2 arrays. first array ($res) contains q_id values $result key , count of each q_id value corresponding values. here's $res if use example:

array {   [question-1] => 3   [question-2] => 3   [question-3] => 2 } 

the second array ($ans) contains questionid values $dataanswers key , count of matching elements of each questionid value corresponding values. here's $ans based on example:

array {   [question-1] => 1   [question-2] => 1   [question-3] => 2 } 

you can see above 2 arrays third element key equals question-3 has same value, expected count, 1. below complete code implements above idea. counter you're looking $matching_count.

// q_id $result , count $res = array(); foreach ($result $r){     if (array_key_exists($r['q_id'], $res)) {         $res[$r['q_id']]++;     }     else {         $res[$r['q_id']] = 1;     } }  // questionid $dataanswers $ans = array();  foreach ($dataanswers $a){     if (!array_key_exists($a['questionid'], $ans)) {         $ans[$a['questionid']] = 0;     } }  foreach ($dataanswers $a){     foreach ($result $r){         if ($a['questionid'] == $r['q_id'] && $a['selectedanswer'] == $r['a_body']) {             // increase count in $ans             $ans[$a['questionid']]++;         }     } }  // check how many elements of $ans have same value $res same key $matching_count = 0; foreach ($ans $key => $value) {     if (array_key_exists($key, $res)         && $ans[$key] == $res[$key]) {         $matching_count++;     } }  echo $matching_count; 

output

1 

working demo: http://codepad.org/5ezyab8l


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -