将多维数组插入MySQL的PHP递归函数

3
我有一个应用程序需要将多维数组存储到多个mysql表中。在遍历数组时,我需要找到特定的键,将值插入到数据库中,并将创建的DB ID传递给数组中的子元素。例如,如果有一个名为“unit”的键,请查找它,获取标题,插入到数据库中,返回ID,将该ID传递到下一次迭代。在下一次迭代中,我从单元中获取ID并搜索课程,使用单元ID保存课程。
这是数组的层次结构:
Unit -Lesson -Quiz --Question ---Answers
以下是发布表格输出的示例。由于用户动态创建表格,因此生成随机ID。我应该坚持for循环还是使用递归来解决我的问题?如果递归在这里是可行的,则非常感谢任何帮助。谢谢。
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
                                            )

                                    )

                            )

                    )

            )

    )
)

1
你知道结构体的最大深度吗? - Chizzle
是的,它结束在答案。 - baneG
1个回答

1

我更倾向于使用递归,因为它是一种更灵活的方法

$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);

谢谢你的回复。我如何区分它是单元/课程/问题还是测验,以知道要使用哪个表。为了测试函数,我只需将$object_id作为$inserted_db_id放置,这将打印出第一个单元的ID,然后我会收到一个错误:“未定义索引:单元”。 - baneG
好的,我明白了。当调用insertItems($object, $inserted_db_id, $structure)时,下一级结构只需将$structure替换为$next_level_structure即可使其继续进行。感谢您的回答,真的帮了我很大的忙!! - baneG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接