dongqiya9552 2018-01-23 15:09
浏览 102

如何使用INSERT QUERY使用多维

I have one multi dimensional array. using this i have to write insert query. i am new in web development. How can write multidimensional array using 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 ;
}

OUTPUT

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

        )

)

I want to insert the record like this using 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看