Search Array PHP -


i have following situation

i have 2 arrys

following answers array

array (      [0] => array (         [id] => 4e28258263d2c4         [answer] => study accounting          [question_id_fk] => 4e28258262d100          [correct] => 0          [result_text] => thats right answer          [order] => 1      )      [1] => array (          [id] => 4e28258266d896          [answer] => new york          [question_id_fk] => 4e28258265b752          [correct] => 0          [score] => 0.00          [result_text] =>          [order] => 1     )      [2] => array (          [id] => 4e282582683870          [answer] => yes          [question_id_fk] => 4e282582674ba0          [correct] => 0          [score] => 0.00          [hot_answer] => 0          [hot_email] =>          [ordering] => 1          [result_text] =>          [order] => 1      )      [3] => array (          [id] => 4e282582698c23          [answer] => 2          [question_id_fk] => 4e282582689e80          [correct] => 0          [score] => 0.00          [hot_answer] => 0          [hot_email] =>          [ordering] => 1          [result_text] =>          [order] => 1      )      [4] => array (          [id] => 4e2825826af072          [answer] => 1          [question_id_fk] => 4e2825826a0371          [correct] => 0          [score] => 0.00          [hot_answer] => 0          [hot_email] =>          [ordering] => 1          [result_text] =>          [order] => 1      )      [5] => array (          [id] => 4e2825826d9638          [answer] => nyc          [question_id_fk] => 4e2825826ca998          [correct] => 0          [score] => 0.00          [hot_answer] => 0          [hot_email] =>          [ordering] => 1          [result_text] =>          [order] => 1      )      [6] => array (          [id] => 4e2825826d9137          [answer] => dallas          [question_id_fk] => 4e2825826ca998          [correct] => 0          [score] => 0.00          [hot_answer] => 0          [hot_email] =>          [ordering] => 1          [result_text] =>          [order] => 1 )  )    

here question list

 array (  [0] => 4e28258262d100 [1] => 4e282582649464 [2] => 4e28258265b752  [3] => 4e282582674ba0 [4] => 4e282582689e80 )      foreach($questionlist $question){         // want answers in array above          // has $question.question_id = question_id_fk . 1 question can have multiple         // answers  //print each question id  //print answer , result_text , correct values answers found answerlist       } 

how search answers question id array . need "correct" , "result_text" values answer .

$matches = array_filter ($answerlist, function ($answer) use ($question) {   return $question['id'] == $answer['question_id_fk']; }); 

now can access every single entry.

if want reduce structures array of primtive values (the values looking for) ca like

$correct = array_reduce(   $matches,    function ($result, $current) {     $result[] = $current['correct'];     return $result;   },    array() ); 

update

i suggest reorder $answerlist

$answerlistorderedbyquestionid = array_reduce (   $answerlist,    function ($result, $answer) {     if (!array_key_exists($answer['question_id_fk'], $result))       $result[$answer['question_id_fk']] = array();      $result[$answer['question_id_fk']][] = $answer;      return $result;   },    array() ); 

this should associative array key question-id. can access every single answer given question.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -