0

I am having this array. Last few hours I have been trying to loop through and make another which will arrange the question in respective subjects.

[23] => Array
        (
            [right] => A list of station and network addresses with corresponding gateway IP address.
            [id] => 23
            [level] => Professional
            [subject] => Array
                (
                    [0] => Array
                        (
                        [tid] => 1
                    )

                [1] => Array
                    (
                        [tid] => 6
                    )

            )

        [question] => What is an IP routing table?
        [answer] => A list of host names and corresponding IP addresses.
        [correct] => 0
    )

[22] => Array
    (
        [right] => Session hijacking attack
        [id] => 22
        [level] => Professional
        [subject] => Array
            (
                [0] => Array
                    (
                        [tid] => 1
                    )

                [1] => Array
                    (
                        [tid] => 6
                    )

            )

        [question] => How would an IP spoofing attack be best classified?
        [answer] => Session hijacking attack
        [correct] => 1
    )

[21] => Array
    (
        [right] => Repeater
        [id] => 21
        [level] => Intermediate
        [subject] => Array
            (
                [0] => Array
                    (
                        [tid] => 1
                    )

                [1] => Array
                    (
                        [tid] => 6
                    )

            )

I want to make something like below.

array ( [tid1] => array (
                            [0] array (  [question] => something )
                            [1] array (  [question] => somethingelse )
                           )
         [tid2] => array (
                            [0] array (  [question] => something )
                            [1] array (  [question] => somethingelse )
                           )

But I'm not able to do it. What is the best way to do this?

2
  • Since it appears that each question has multiple subjects, will the questions appear multiple times in the final array (once under each tid)? Commented Feb 24, 2012 at 20:05
  • Yes. It will. I want to list questions by tid. Commented Feb 24, 2012 at 20:08

1 Answer 1

1

Assuming that your data is in an array called $data and that since each question can have multiple subjects, each question may appear multiple times in the final array:

$final = array();
foreach($data as $datum){
    $subjects = $datum['subject'];
    foreach($subjects as $subject){
        $tid = 'tid' . $subject['tid'];
        $final[$tid][] = array('question' => $datum['question']);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.