通过平面白名单数组过滤二维数组
我有以下情况
我有 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”值。
I have following situation
I have 2 arrys
Following is answers array
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 )
)
Here is Question List
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
}
How do I search all answers for a question id from array . I need to get "correct" and "result_text" values for an answer .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在您可以访问每个条目。
如果您想将结构减少为原始值数组(您正在寻找的值),您可以执行类似
更新的
操作,我建议重新排序您的
$answerList
这现在应该是一个关联数组关键是问题 ID。现在您可以轻松访问给定问题的每个答案。
Now you can access every single entry.
If you want to reduce the structures to an array of primtive values (the values you are looking for) you ca do something like
Update
I would suggest to reorder your
$answerList
This should now be an associative array with the key the question-id. Now you can easily access every single answer for a given question.
如果我正确理解你的评论:
If I understand your comment correctly:
该示例仅显示答案列表数组?
另一方面,您可以将answerlist 处理一次,放入由question_id_fk 索引的数组中,而不是在列表中搜索每个问题。
The example only shows the answerlist array?
On the other hand, you could process answerlist once, into an array indexed by question_id_fk, rather than searching the list for each question.