Cakephp:foreach循环中的两个数组?

时间:2013-08-05 15:20:52

标签: arrays cakephp foreach

我有2个数组。 1从数据库中获取,而另一个来自视图页面。我正在开发一个在线考试网站,所以我必须检查用户输入的答案是否正确。这是第一个。

 Array
    (
        [0] => Array
            (
                [Question] => Array
                    (
                        [id] => 51f92e34-c5a8-4de3-b264-0ff0d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [qus] => What is ur name?
                        [slug] => name-find
                        [image] => 
                        [opt1] => x
                        [opt2] => y
                        [opt3] => a
                        [opt4] => b
                        [opt5] => c
                        [answer_id] => 4
                        [description] => 
                        [ansimage] => 
                    )

                [Aptitude] => Array
                    (
                        [id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => php
                        [slug] => php
                    )

                [Paper] => Array
                    (
                        [id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => aptitude1
                        [slug] => aptitude1
                    )

                [Answer] => Array
                    (
                        [id] => 4
                        [name] => D
                    )

            )

        [1] => Array
            (
                [Question] => Array
                    (
                        [id] => 51fe4098-c344-4790-9e46-0fb4d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [qus] => Place?
                        [slug] => place
                        [image] => 
                        [opt1] => ss
                        [opt2] => sss
                        [opt3] => ss
                        [opt4] => ss
                        [opt5] => ss
                        [answer_id] => 3
                        [description] => 
                        [ansimage] => 
                    )

                [Aptitude] => Array
                    (
                        [id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => php
                        [slug] => php
                    )

                [Paper] => Array
                    (
                        [id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => aptitude1
                        [slug] => aptitude1
                    )

                [Answer] => Array
                    (
                        [id] => 3
                        [name] => C
                    )

            )

        [2] => Array
            (
                [Question] => Array
                    (
                        [id] => 51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [paper_id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [qus] => hayywep?
                        [slug] => dada
                        [image] => 
                        [opt1] => a
                        [opt2] => a
                        [opt3] => a
                        [opt4] => a
                        [opt5] => a
                        [answer_id] => 3
                        [description] => 
                        [ansimage] => 
                    )

                [Aptitude] => Array
                    (
                        [id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => php
                        [slug] => php
                    )

                [Paper] => Array
                    (
                        [id] => 51f924cc-a158-441e-9119-0ff0d0483c4c
                        [aptitude_id] => 51f92441-d510-4c3d-85e3-0ff0d0483c4c
                        [name] => aptitude1
                        [slug] => aptitude1
                    )

                [Answer] => Array
                    (
                        [id] => 3
                        [name] => C
                    )

            )

    )

and 



    Array
    (
        [51f92e34-c5a8-4de3-b264-0ff0d0483c4c] => 3
        [51fe4098-c344-4790-9e46-0fb4d0483c4c] => 3
        [51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c] => 3
    )

我写了这段代码

foreach($res as $res1): 

foreach($ans as $ans1):
if($res1['Question']['answer_id']==$ans1)
{
print_r($res1['Question']['id']);

}
endforeach;
endforeach;

输出:

51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51f92e34-c5a8-4de3-b264-0ff0d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe4098-c344-4790-9e46-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c
51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c

如何删除重复?

1 个答案:

答案 0 :(得分:1)

尝试array_unique

$_results = array();
    foreach($res as $res1): 

    foreach($ans as $ans1):
    if($res1['Question']['answer_id']==$ans1)
    {
    $_results[] = $res1['Question']['id'];

    }
    endforeach;
    endforeach;

    $results = array_unique($_results);
    pr($results);