Loop and Insert PHP multidimensional array in mysql

I have a multidimensional array in php that look like this:

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 key "day" is sent to the day, so the day [0] = "Monday" And the key "periods" repeats the hour that the user selected on the same day.

So, on day [0] there is an array of "periods" that refers to an anthor array that stores an hour, it starts at 01:00, the end is 01:30 the beginning of 02:30, the end is 03:00

I am trying to use this array but cannot find a way. I want to enter this value for each hour in mysql as follows:

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

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

any suggestions?

+5
source share
1 answer

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; 
0
source

Source: https://habr.com/ru/post/1269779/


All Articles