PHP:在mysql db中插入多维关联数组

时间:2017-05-02 12:30:13

标签: php mysql multidimensional-array associative-array

我有一个包含商店和工作时间的数组:

[0] => Array
(
[name] => Value1

[working_hours] => Array
            (
                [sunday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 6:00
                            )

                    )

                [saturday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 6:00
                            )

                        [1] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

                [tuesday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 4:00
                            )

                        [1] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

                [friday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 4:00
                            )

                        [1] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

                [thursday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 4:00
                            )

                        [1] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

                [wednesday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 00:00
                                [1] => 4:00
                            )

                        [1] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

                [monday] => Array
                    (
                        [0] => Array
                            (
                                [0] => 19:30
                                [1] => 23:59
                            )

                    )

            )

这是我必须在db中插入的数百条记录。 我这样做了我的bd表: hours_id INT,shop INT,day INT或VARCHAR(没问题),openTime TIME,closeTime TIME。 到目前为止我的代码是这样的:     ...... //代码在这里

foreach ($row["hours"] as $h1) {
      $day = $h_days["?"]; //how to put it here?
      foreach ($h1 as $h2) {
        foreach ($h2 as $hour) {
            $openTime = $hour["0"];
            $closeTime = $hour["1"];
            $insert_items4 = mysql_query("INSERT INTO hours (shop, day, openTime, closeTime) VALUES ('".$var1."', '". $day."', '".$openTime."', '".$closeTime."')");

        }
      }
    }    

我在array_map中找到了一些例子,例如$ days = array_map('mysql_real_escape_string',array_values($ row [“hours”])); 或类似这样的$ keys = implode(',',array_keys($ row [“hours”]));但我不知道如何正确使用它们。如何在开启和关闭时间插入每条记录的日期?我搜索了但是找不到这样的东西。提前致谢

0 个答案:

没有答案