0

私は2つの配列を持っています。1 つはデータベースから取得し、もう 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

重複を削除するには?

4

1 に答える 1

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);
于 2013-08-05T19:21:41.393 に答える