Recursive function to insert multidimensional array in PHP mysql
I have an application that needs to store a multidimensional array in multiple mysql tables. When iterating through an array, I need to find specific keys, insert that value into the database and pass the generated database id to the child in the array. For example, if there is a key "one", find it, get the title, insert into the database, return the id, pass that id to the next iteration. In the next iteration, I get the ID from the device and search for the lesson, save the lesson with the module ID.
This is an array hierarchy.
Unit
-Lesson
-Quiz
--Question
--- Answers
Here is an example of a post form output There are randomly generated IDs because the form is created dynamically by the user. Should I stick with loops or use recursion to solve my problem? If recursion is possible here, any help is greatly appreciated. Thank.
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
)
)
)
)
)
)
)
source to share
I would prefer recursion as a 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);
source to share