我有一个多维数组使用这个数组我必须采取第一个和最后一个索引意味着爆炸值和剩余索引我们必须给静态10小时
$sql = "SELECT DISTINCT date_add(date(d_started_on), interval i day) as allocation_date,t_project,t_assign_to,t_start_time,t_end_time FROM ( SELECT date(STR_TO_DATE(t_started_on, '%Y-%m-%d')) as d_started_on, datediff( date(STR_TO_DATE(t_due_on, '%Y-%m-%d')), date(STR_TO_DATE(t_started_on, '%Y-%m-%d')) ) as days,t_project,t_assign_to,t_start_time,t_end_time FROM task ) as base INNER JOIN nums ON i <= days ORDER BY 1";
$mysql = mysql_query($sql);
$productsArray = array();
while($rowRes = mysql_fetch_assoc($mysql)){
$rowRes['allocated_day'] = $rowRes['allocation_date'];
$productsArray[] = $rowRes;
}
$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'];
}
OUTPUT print_r($ project_name);
Array
(
[G2E0357] => Array
(
[10001] => Array
(
[2018-01-01] => 01:30 PM-02:30 PM
)
[10002] => Array
(
[2018-01-25] => 10:30 AM-01:30 PM
[2018-01-26] => 10:30 AM-01:30 PM
[2018-01-27] => 10:30 AM-01:30 PM
)
)
[XPL0315] => Array
(
[10002] => Array
(
[2018-01-18] => 11:30 AM-07:30 PM
[2018-01-19] => 11:30 AM-07:30 PM
[2018-01-20] => 11:30 AM-07:30 PM
)
)
)
现在我想采用 t_project 数组,
在这个数组中,第一个索引意味着,我必须爆炸-
并且我显示爆炸第0个索引值然后allocate_day数组最后一个索引意味着我必须显示爆炸第一个索引值
预期产出
Array
(
[G2E0357] => Array
(
[10001] => Array
(
[2018-01-01] => 01:30 PM
[2018-01-02] => 10 Hrs
[2018-01-03] => 11:30 AM
)
[10002] => Array
(
[2018-01-25] => 10:30
[2018-01-26] => 10 Hrs
[2018-01-27] => 01:30 PM
)
)
[XPL0315] => Array
(
[10002] => Array
(
[2018-01-18] => 11:30 AM
[2018-01-19] => 10 Hrs
[2018-01-20] => 07:30 PM
)
)
)
更新了预期答案
Array
(
[G2E0357] => Array
(
[10001] => Array
(
[2018-01-01] => 2 Hrs
)
[10002] => Array
(
[2018-01-25] => 9 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 4 Hrs
)
)
[XPL0315] => Array
(
[10002] => Array
(
[2018-01-18] => 8 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 10 Hrs
)
)
)
答案 0 :(得分:1)
使用您创建的数组,您可以使用array_pop
和array_unshift
来获取第一个和最后一个元素。然后你可以迭代剩余的元素。
$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);
$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 ;
}
}
print_r($project_name);
/**
* @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
)
[10002] => Array
(
[2018-01-25] => 9 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 4 Hrs
)
)
[XPL0315] => Array
(
[10002] => Array
(
[2018-01-18] => 8 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 10 Hrs
)
)
)
答案 1 :(得分:1)
这是一个更新的anwser:
NB:01:30 PM-02:30 PM = 1,而不是2
$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) ;
// create a new array :
$per_assign = [] ;
if (count($dates) > 1)
{
// get first and last dates :
$first = reset($days);
$last = end($days);
// remove first and last dates
$first_time = array_shift($dates) ;
$last_time = array_pop($dates) ;
// 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';
}
else
{
$first_time = array_shift($dates) ;
list($begin,$end) = explode("-", $first_time) ;
$begin_tm = compute_am_pm_time($begin) ;
$end_tm = compute_am_pm_time($end) ;
$first = reset($days) ;
$per_assign[$first] = compute_duration($end_tm, $begin_tm) . ' Hrs';
}
// assign new values :
$project_name[$assign][$t_proj] = $per_assign ;
}
}
print_r($project_name);
/**
* @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) ;
$is_pm = strtolower($part) == 'pm' ;
$hrs = ((int)$hrs) + ($is_pm && $hrs != '12' ? 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] => 1 Hrs
)
[10002] => Array
(
[2018-01-25] => 9 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 4 Hrs
)
)
[XPL0315] => Array
(
[10002] => Array
(
[2018-01-18] => 8 Hrs
[2018-01-26] => 10 Hrs
[2018-01-27] => 10 Hrs
)
)
)
答案 2 :(得分:0)
由于您需要更改的数据格式和长度相同,我只需执行以下操作:
foreach ($project_name as $key => $value) {
$value[0] = substr($value[0], 0, 8);
$value[1] = "10 Hrs";
$value[2] = substr($value[2], 9);
}