如何使用INSERT QUERY使用多维

时间:2018-01-23 15:09:33

标签: php mysql arrays multidimensional-array

我有一个多维数组。使用这个我必须写插入查询。我是网络开发的新手。如何使用INSERT QUERY

编写多维数组
date_default_timezone_set('UTC');
$start_date = "2018-01-01";
$start_time = "01:30 PM";
$due_date="2018-01-03";
$due_time = "11:30 AM";
$project="10001";
$assign_to="G2E0357";

// make start date in seconds
$ex = explode('-', $start_date);
$start = mktime(0,0,0,$ex[1],$ex[2],$ex[0]);

// make end date in seconds
$ex = explode('-', $due_date);
$end = mktime(0,0,0,$ex[1],$ex[2],$ex[0]);

$productsArray = array();
$counter=0;
while($start <= $end)
{
   $productsArray[$counter] = array();
   // convert the time to date for output
   $productsArray[$counter]['allocation_date'] = date("Y-m-d", $start);
   $productsArray[$counter]['t_project'] = $project;
   $productsArray[$counter]['t_assign_to'] = $assign_to;
   $productsArray[$counter]['t_start_time'] = $start_time;
   $productsArray[$counter]['t_end_time'] = $due_time;
   $productsArray[$counter]['allocated_day'] = date("Y-m-d", $start);
   $counter++;
   // add a day to $start
   $start += 24*3600;
}

$project_name = array();
foreach($productsArray as $key0 => $info) {
    $key1 = $info['t_assign_to'];
    $key2 = $info['t_project'];
    $key3 = $info['allocated_day'];
    $project_name[$key1][$key2][$key3] = $info['t_start_time'].'-'.$info['t_end_time']; 
} 

$office_start = compute_am_pm_time("9:30 AM") ;
$office_end = compute_am_pm_time("7:30 PM") ;
foreach ($project_name as $assign => $infos) {
foreach ($infos as $t_proj => $dates) {
    $days = array_keys($dates) ;


    // get first and last dates :
    $first = reset($days); //2018-01-01 2018-01-25 2018-01-18
    $last = end($days);
    // remove first and last dates
    $first_time = array_shift($dates) ;
    $last_time = array_pop($dates) ;

    // create a new array :
    $per_assign = [] ;

    // get first date
    $begin = substr($first_time, 0,strpos($first_time,'-'));
    $begin_tm = compute_am_pm_time($begin) ;
    $per_assign[$first] = compute_duration($office_end, $begin_tm) . ' Hrs';

    // get static 10hrs (but could be computed)
    foreach ($dates as $day => $time) {
        $per_assign[$day] = '10 Hrs';
    }

    // get last date
    $end = substr($last_time, strpos($last_time,'-')+1);
    $end_tm = compute_am_pm_time($end) ;
    $per_assign[$last] = compute_duration($end_tm, $office_start) . ' Hrs';

    // assign new values :
    $project_name[$assign][$t_proj] = $per_assign ;
}
}

/**
 * @param $time_str A time in 12H format ("09:30 AM").
 * @returns the number of minutes since 00:00
 */
function compute_am_pm_time($time_str)
{
    list($time, $part) = explode(' ', $time_str, 2);
    list($hrs, $mins) = explode(':', $time) ;
    $hrs = ((int)$hrs ) + (strtolower($part) == 'pm' ? 12 : 0);
    return $hrs*60 + $mins ;
}
function compute_duration($end_min, $begin_min) {
    return ($end_min - $begin_min) / 60 ;
}

输出

 Array
(
    [G2E0357] => Array
        (
            [10001] => Array
                (
                    [2018-01-01] => 6 Hrs
                    [2018-01-02] => 10 Hrs
                    [2018-01-03] => 2 Hrs
                )

        )

)

我想使用print_r($ project_name);

插入这样的记录
Id    empId          projectId          date         workingHrs
1     G2E357          10001             2018-01-01     6 Hrs
2     G2E357          10001             2018-01-02     10 Hrs
3     G2E357          10001             2018-01-03     2 Hrs

0 个答案:

没有答案