0

I have 2 arrays. 1 fetching from database and other one is from view page. I am developing a online test exam website so i have to check if the answer entered by user is correct or not.Here is the 1st one.

 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
    )

i wrote this code

foreach($res as $res1): 

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

}
endforeach;
endforeach;

Output:

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

how to remove the duplication?

2
  • When you were typing your question, there was a How to Format box to the right of the text area. It's well worth giving that a read, and using the preview area underneat the text area to make sure your question is formatted in a way that people can readily read and answer it. I've formatted the code blocks and such for you on this occasion. Commented Aug 5, 2013 at 15:24
  • It's also best to open with a concise summary of your question, and then present the code/data/markup/etc., so people have some context. Commented Aug 5, 2013 at 15:25

1 Answer 1

1

Try 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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for the answer Tim Joyce. But i get the following result after executing your code . Array ( [0] => 51f92e34-c5a8-4de3-b264-0ff0d0483c4c [3] => 51fe4098-c344-4790-9e46-0fb4d0483c4c [6] => 51fe40ad-9ddc-4f07-94dc-0fb4d0483c4c )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.