循环并将PHP多维数组插入到mysql中

时间:2021-12-12 01:43:14

I have a multi-dimensional array in php that look like this :

我在php中有一个多维数组,如下所示:

 Array
    (
    [0] => Array
    (
        [day] => 0
        [periods] => Array
            (
                [0] => Array
                    (
                        [0] => 01:00
                        [1] => 01:30
                    )

                [1] => Array
                    (
                        [0] => 02:30
                        [1] => 03:00
                    )

            )

    )

[1] => Array
    (
        [day] => 1
        [periods] => Array
            (
                [0] => Array
                    (
                        [0] => 01:30
                        [1] => 02:00
                    )

            )

    )

The "day" key reffer to the day so day[0] = "monday" And the "periods" key reffer to the hour that the user has selected on the same day.

“day”密钥响应当天[0] =“星期一”并且“句点”密钥响应用户在同一天选择的小时。

So in the day[0] there is a 'periods' array that reffer to anthor array that store the hour , start 01:00, end 01:30 start 02:30, end 03:00

所以在白天[0]有一个'周期'数组,对于存储小时的anthor数组,开始01:00,结束01:30开始02:30,结束03:00

I try to foreach loop this array but I can not find a way. I want to enter this value for each hour to mysql like so:

我尝试foreach循环这个数组但我找不到方法。我想每小时输入这个值到mysql,如下所示:

    $sql  = "INSERT INTO task_list (
            task, day, hour
            ) VALUES (?, ?, ?)
    ";  

ex : day: 0 start 1:00 , day: 0 end 1:30

例如:日:0开始1:00,日:0结束1:30

any suggestions?

1 个解决方案

#1


0  

Here is your Solution....

这是你的解决方案......

$array = array(
        array(
            'day' => 0,
            'periods' => array(
                array('01:00','01:30'),
                array('02:00','03:00'),
            )
        ),
        array(
            'day' => 1,
            'periods' => array(
                array('01:00','02:00')
            )
        )
    );

   //echo "<pre>";print_r($array);

   $values = '"INSERT INTO task_list ( task, day, hour ) VALUES';
   foreach($array as $row){
        foreach($row['periods'] as $row1){
                $values .= '("start","'.$row['day'].'","'.$row1[0].'"),("end","'.$row['day'].'","'.$row1[1].'"),';
        }
   }
   $values .= ';"';
   $values = str_replace(',;',';',$values);
   echo $values;

#1


0  

Here is your Solution....

这是你的解决方案......

$array = array(
        array(
            'day' => 0,
            'periods' => array(
                array('01:00','01:30'),
                array('02:00','03:00'),
            )
        ),
        array(
            'day' => 1,
            'periods' => array(
                array('01:00','02:00')
            )
        )
    );

   //echo "<pre>";print_r($array);

   $values = '"INSERT INTO task_list ( task, day, hour ) VALUES';
   foreach($array as $row){
        foreach($row['periods'] as $row1){
                $values .= '("start","'.$row['day'].'","'.$row1[0].'"),("end","'.$row['day'].'","'.$row1[1].'"),';
        }
   }
   $values .= ';"';
   $values = str_replace(',;',';',$values);
   echo $values;