搜索数组PHP

时间:2011-07-21 13:58:25

标签: php arrays

我有以下情况

我有2个arrys

以下是答案数组

Array ( 
    [0] => Array (
        [id] => 4e28258263d2c4
        [answer] => Study Accounting 
        [question_id_fk] => 4e28258262d100 
        [correct] => 0 
        [result_text] => Thats a 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 ) 
)

 

这是问题清单

 Array ( 
[0] => 4e28258262d100
[1] => 4e282582649464
[2] => 4e28258265b752 
[3] => 4e282582674ba0
[4] => 4e282582689e80 )

    foreach($questionList as $question){
        // I want to Get answers in array above which 
        // has $question.question_id = question_id_fk . One question can have multiple
        // answers

//Print Each question Id 
//Print answer , result_text and correct values from all answers found from AnswerList

     }

如何从数组中搜索问题ID的所有答案。我需要获得答案的“正确”和“result_text”值。

3 个答案:

答案 0 :(得分:2)

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

现在您可以访问每个条目。

如果你想将结构减少到一组原始值(你正在寻找的值),你可以做类似的事情

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

更新

我建议您重新排序$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()
);

现在应该是一个关联数组,其关键字为question-id。现在,您可以轻松访问给定问题的每个答案。

答案 1 :(得分:1)

foreach ($questionList as $question)
{
  if ($question['id'] == /*id we want to match*/)
  {
    // $question is now your match
    //   $question['correct']
    //   $queestion['result_text']
  }
}

如果我理解你的评论:

// go through each aquestion
foreach ($questionList as $question)
{
  // for each question, we want the answer associated with it
  foreach ($answerList as $answer)
  {
    // make sure the ids match up
    if ($answer['question_id_fk'] == $question)
    {
      // $question & $answer is now your pair
    }
  }
}

答案 2 :(得分:-1)

该示例仅显示应答列表数组?

foreach ($questionList as $question)
{
  // for each question, look through the answers...
  foreach ($answerlist as $answer)
    if ($answer['question_id_fk'] == $question['id'])
    {
      // $answer matches the current question
    }
  }
}

另一方面,您可以将answerlist处理到一个由question_id_fk索引的数组中,而不是在列表中搜索每个问题。

相关问题