3

I have an app that needs to store a multidimensional array into several mysql tables. When iterating through the array i need to find specific keys , insert the value into the database and pass the created DB ID to the child element in the array. For instance if there is a key "unit" find it , get the title , insert into database, return the id , pass that id to the next iteration. In the next iteration i get the id from the unit and search for a lesson , save the lesson with the unit id.

This is the hierarchy of the array.

Unit
-Lesson
-Quiz
--Question
---Answers

Here is an example of the output of the post form. There are randomly generated ids because the form is created dynamically by the user. Should i stick with for loops or use recursion to solve my problem? If recursion is possible here any help is much appreciated. Thank you.

Array
(
[unit] => Array
    (
        [fa53a225-e10c-408b-ad98-f3be26670587] => Array
            (
                [title] => Unit 1
                [lesson] => Array
                    (
                        [ae89d2bd-bb06-42ed-be5d-76d450fa1d68] => Array
                            (
                                [title] => Lesson 1
                            )

                        [79245a3a-e3e8-4aa2-9b5b-35cef4740d93] => Array
                            (
                                [title] => Lesson 2
                            )

                        [34c30554-3b4c-4c5f-b398-4dbc7a2f2d00] => Array
                            (
                                [title] => Lesson 3
                            )

                        [28241d75-1733-47e1-aa34-133bc71ef382] => Array
                            (
                                [title] => Lesson 4
                            )

                    )

                [quiz] => Array
                    (
                        [e93b5973-e13d-4a2d-a60a-4f86721b8f5a] => Array
                            (
                                [title] => Quiz 1
                                [question] => Array
                                    (
                                        [5e9d4f74-af08-430d-a405-e4d5464aff4a] => Array
                                            (
                                                [title] => Question 1
                                                [type] => truefalse
                                                [answer] => false
                                            )

                                        [b848cd75-bae4-44dd-99b0-ba041cd74b87] => Array
                                            (
                                                [title] => Question 2
                                                [type] => truefalse
                                                [answer] => true
                                            )

                                        [f5c72134-2de2-4fc4-8601-1776e43461e9] => Array
                                            (
                                                [title] => Question 3
                                                [type] => multiple
                                                [correct] => Array
                                                    (
                                                        [0] => 0
                                                        [1] => 1
                                                    )

                                                [answer] => Array
                                                    (
                                                        [0] => answer 1
                                                        [1] => answer 2
                                                        [2] => answer 3
                                                        [3] => 
                                                        [4] => 
                                                    )

                                            )

                                    )

                            )

                    )

            )

        [a8a42316-f5fb-4b44-bd41-e19e8f3fb8e0] => Array
            (
                [title] => Unit 2
                [lesson] => Array
                    (
                        [b438f957-7386-4202-8ff0-61fcdb020ba6] => Array
                            (
                                [title] => Lesson 1
                            )

                        [c6513c26-2d2f-4835-8fe7-9f4c82ea459d] => Array
                            (
                                [title] => Lesson 2
                            )

                        [d6853af6-e3a8-4e17-9df8-eeddb5859483] => Array
                            (
                                [title] => Lesson 3
                            )

                    )

                [quiz] => Array
                    (
                        [96c1b4c2-1e00-4702-9064-25fcea6e10bb] => Array
                            (
                                [title] => Quiz 1
                                [question] => Array
                                    (
                                        [fc44b82e-089c-47b0-8404-9c12a9ecb2d6] => Array
                                            (
                                                [title] => question 1
                                                [type] => truefalse
                                                [answer] => true
                                            )

                                    )

                            )

                    )

            )

    )
)
2
  • 1
    Do you know the maximum depth of the structure? Commented Jul 28, 2015 at 2:49
  • Yes , it ends at answer. Commented Jul 28, 2015 at 3:09

1 Answer 1

1

I would prefer recursion, as more flexible approach

$structure = ["Unit"=>
                      ["Lesson"=>[],
                       "Quiz"=>
                               ["Question"=>
                                            ["Answers"=>[]]]]];

function insertItems($array, $parent_db_id, $structure) {

   // at first step $object_name=Unit, at second Lesson, quiz
   foreach ($structure as $object_name=>$next_level_structure) {

     // loop through objects (at first step units, at second Lessons, quizes)
     foreach ($array[$object_name] as $object_id=>$object) {

       // ... insert in db $object_id, $object['title'], $parent_db_id etc
       // retrieve $inserted_db_id ...

       // next recursion level if next level not empty
       if ($next_level_structure) {
          insertItems($object, $inserted_db_id, $next_level_structure);
          }
       }  
     }

}

insertItem($data, null, $structure);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply. How do I differentiate if it is a unit / lesson / question or quiz to know which table to use. For testing the function i just put the $object_id as the $inserted_db_id , this prints out the id of the first unit and then i get an error : " Undefined index: unit "
Ok, i got it. when calling insertItems($object, $inserted_db_id, $structure); for the next level structure just replaced $structure with $next_level_structure so it continiues to go through . Thanks for the answer. Really helped!!

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.