循环遍历一个数组并在不同的多维数组的子数组中查找值

时间:2015-03-04 02:06:59

标签: php arrays search foreach

我在这上面撞墙!

我有两个数组:

$questionArray = Array(
    [0] => Array
        (
            [QueID] => 53
            [QueType] => Map question
            [QueText] => Where do you live?
        )
    [1] => Array
        (
            [QueID] => 55
            [QueType] => Multi selection
            [QueText] => What's your favorite car?
        )
    [2] => Array
        (
            [QueID] => 59
            [QueType] => Multi selection
            [QueText] => What's your favorite fruit?
        )
    [3] => Array
        (
            [QueID] => 54
            [QueType] => Single selection
            [QueText] => What's your favorite color?
        )
)

$answerArray = Array(
    [52264f64c9482] => Array
        (
            [0] => Array
                (
                    [QueID] => 53
                    [QueType] => Map question
                    [Lat] => 41.86
                    [Lng] => -87.77
                    [AnsText] => 
                )
            [1] => Array
                (
                    [QueID] => 55
                    [QueType] => Multi selection
                    [Lat] => 
                    [Lng] => 
                    [AnsText] => Chevy
                )
            [2] => Array
                (
                    [QueID] => 54
                    [QueType] => Single selection
                    [Lat] => 
                    [Lng] => 
                    [AnsText] => Green
                )
        )
    [522a522e9f41f] => Array
        (
            [0] => Array
                (
                    [QueID] => 53
                    [QueType] => Map question
                    [Lat] => 51.41
                    [Lng] => -27.21
                    [AnsText] => 
                )
            [1] => Array
                (
                    [QueID] => 55
                    [QueType] => Multi selection
                    [Lat] => 
                    [Lng] => 
                    [AnsText] => Ford
                )
            [2] => Array
                (
                    [QueID] => 59
                    [QueType] => Multi selection
                    [Lat] => 
                    [Lng] => 
                    [AnsText] => Banana
                )
        )
    [522a527ec0115] => Array
        (
            [0] => Array
                (
                    [QueID] => 54
                    [QueType] => Single selection
                    [Lat] => 
                    [Lng] => 
                    [AnsText] => Blue
                )
        )
)

我试图遍历$ answerArray中的每个数组,然后对于$ questionArray中的每个问题数组,如果在$ answerArray的子​​数组中有一个或者在$的子数组中没有匹配的答案,则显示答案answerArray返回"没有答案"值。因此,如果脚本工作,它将采用上面的两个数组并返回:

你住在哪里?:     Lat - 41.86
    Lng - -87.77
你最喜欢的车是什么? - 雪佛兰 你最喜欢的水果是什么? - 没有答案
你最喜欢的颜色是什么? - 绿色

你住在哪里?:     Lat - 51.41
    Lng - -27.21
你最喜欢的车是什么? - 福特
你最喜欢的水果是什么? - 香蕉
你最喜欢的颜色是什么? - 没有答案

你住在哪里?:     Lat - 没有答案
    Lng - 没有答案
你最喜欢的车是什么? - 没有答案
你最喜欢的水果是什么? - 没有答案
你最喜欢的颜色是什么? - 蓝色

我无处可去,我所管理的最接近的是:

foreach ($answerArray as $ansArray) {
    foreach ($questionArray as $queArray) {
        foreach ($ansArray as $ansItemArray) {
            if ($ansItemArray['QueID'] == $queArray['QueID']) {
                $result = $ansItemArray;
                if ($result['SQueType']== 'Map question') {
                    echo $ansItemArray['QueText'].' - '.$result['Lat'].'</br>';
                    echo $ansItemArray['QueText'].' - '.$result['Lng'].'</br>';
                } else {
                    echo $ansItemArray['QueText'].' - '.$result['AnsText'].'</br>';
                }
            } else {
                $result = array();
                if ($result['SQueType']== 'Map question') {
                    echo $ansItemArray['QueText'].' - No Answer</br>';
                    echo $ansItemArray['QueText'].' - No Answer</br>';
                } else {
                    echo $ansItemArray['QueText'].' - No Answer</br>';
                }
            }
        }
    }
    echo '</br></br>';
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

必填:

  • 格式用户提供的答案
  • 为用户指出未回答的问题

基本上,这是一个单一的传递提供的答案&#39;。

我们需要指出哪些问题没有得到解答。我这样做的方法是拥有一个状态向量&#39;所有的问题,并在他们得到回答时取消它们。关于剩下的报告。

工作代码:CodePad.org

驾驶密码:

foreach ($answerArray as $id => $oneSetOfAnswers) {

    $unasweredQuestions = array();  // need a copy as it is a 'state vector'
                                    // of 'processed questions for this individual.
    foreach($questionArray as $oneQuestion) {
        $unasweredQuestions[$oneQuestion['ueID']] = $oneQuestion;
    }

    // process the answers...
    echo '<br />', '[' . $id . ']';

    foreach ($oneSetOfAnswers as $id => $oneAnswer) {


        $queId = $oneAnswer['QueID'];

        $theAnswers = getAnswer($unasweredQuestions[$queId], $oneAnswer);

        // remove from the list
        unset($unasweredQuestions[$queId]);

        // show answers
        foreach($theAnswers as $oneLine) {
            echo '<br />', $oneLine;
        }
    }

    // show the unansweredQuestions
    foreach ($unasweredQuestions as $question) {
        echo '<br />', $question['ueText'] .' : no answer supplied.';
    }
}

提取一个问题的答案的功能。

/**
 *
 * @param type $question
 * @param type $oneAnswer
 * @return array ready to print
 */
function getAnswer($question,  $oneAnswer)
{
    $useAnsText =    strpos(strtolower($question['ueType']), 'multi') !== false
                  || strpos(strtolower($question['ueType']), 'single') !== false;
    $q = $question['ueText'];

    if ($useAnsText) {
        return array($q . ' : '. $oneAnswer['AnsText']);
    }

    // ignore standard stuff and assume whatever remains is wanted
    $ignore = array('AnsText', 'QueID', 'QueType');

    $answer = array($q);

    foreach($oneAnswer as $id => $response) {
        if (in_array($id, $ignore)) {
            continue;
        }

        $answer[] = $id .' : '. $response;
    }
    return $answer;
}