1 回答
TA贡献1863条经验 获得超2个赞
我会这样做(使用array_intersect):
$question = 'how old are you';
$question_array = explode(' ',$question);
$answers_array[0]['keywords'] = array('old', 'are');
$answers_array[0]['answer'] = 'this is my age';
$answers_array[1]['keywords'] = array('yes', 'no');
$answers_array[1]['answer'] = 'this is a question';
$answers_array[2]['keywords'] = array('tall', 'short');
$answers_array[2]['answer'] = 'this are my measures';
$answers_array[3]['keywords'] = array('back', 'blue');
$answers_array[3]['answer'] = 'My colorus';
foreach($answers_array as $key=> $answer)
{
if(sizeof(array_intersect($answers_array[$key]['keywords'], $question_array)) == sizeof($answers_array[$key]['keywords']))
{
$final_answer = $answers_array[$key]['answer'];
}
}
echo $final_answer;
- 1 回答
- 0 关注
- 139 浏览
添加回答
举报