我有一个如下所示的数组:
[special_days_nested_array] => Array
(
[0] => Array
(
[day_name] => Mon
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[1] => Array
(
[day_name] => Wed
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[2] => Array
(
[day_name] => Fri
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[3] => Array
(
[day_name] => Sun
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
)
案例1: 当我遍历此数组以查找重复项时 - 两个时间相同。 例如,在这些日子里所有的时间都是相同的,我应该得到我存储天数的数组:
[special_days_same_time_array] => Array
(
[0] => Mon
[1] => Wed
[2] => Fri
[3] => Sun
)
但是,相反,我得到这样的东西:
[special_days_same_time_array] => Array
(
[0] => Mon
[1] => Wed
[2] => Wed
[3] => Fri
[4] => Fri
[5] => Sun
)
案例2: 当我的嵌套数组看起来像这样:
[special_days_nested_array] => Array
(
[0] => Array
(
[day_name] => Mon
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[1] => Array
(
[day_name] => Tue
[start_time] => 12:00 am
[end_time] => 09:00 pm
)
[2] => Array
(
[day_name] => Wed
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
)
同一时间不是连续几天,但隔天,我的数组仍为空(或根本没有数组) 案例3: 当两天有相同的时间,而其他两天有相同的时间,这样的事情:
[special_days_nested_array] => Array
(
[0] => Array
(
[day_name] => Mon
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[1] => Array
(
[day_name] => Tue
[start_time] => 01:00 pm
[end_time] => 07:00 pm
)
[2] => Array
(
[day_name] => Wed
[start_time] => 12:00 am
[end_time] => 09:00 pm
)
[3] => Array
(
[day_name] => Thu
[start_time] => 12:00 am
[end_time] => 09:00 pm
)
)
我只得到一次:
[special_timings_same_time] => 12:00 am - 09:00 pm
而且,同一天的数组是这样的:
[special_days_same_time_array] => Array
(
[0] => Mon
[1] => Tue
[2] => Wed
[3] => Thu
)
所有这些日子的时间都是凌晨12点到晚上9点
这是我的代码,我尝试在相同时间和不同时间数组之间进行排序。
//Special timings to check for the same time of two days
//$j = 0;
for($j = 0; $j < count($data_set->special_days_nested_array)-1; $j++){
if(($data_set->special_days_nested_array[$j]['start_time'] == $data_set->special_days_nested_array[$j+1]['start_time']) && ($data_set->special_days_nested_array[$j]['end_time'] == $data_set->special_days_nested_array[$j+1]['end_time']) ) {
//Days Array
$data_set->special_days_same_time_array[] = $data_set->special_days_nested_array[$j]['day_name'];
$data_set->special_days_same_time_array[] = $data_set->special_days_nested_array[$j+1]['day_name'];
//timings
$data_set->special_timings_same_time = $data_set->special_days_nested_array[$j]['start_time'].' - '.$data_set->special_days_nested_array[$j]['end_time'];
}
else{
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$j+1]['day_name']]['day_name'] = $data_set->special_days_nested_array[$j+1]['day_name'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$j+1]['day_name']]['start_time'] = $data_set->special_days_nested_array[$j+1]['start_time'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$j+1]['day_name']]['end_time'] = $data_set->special_days_nested_array[$j+1]['end_time'];
}
//$j++;
}
我如何处理这两种情况?
答案 0 :(得分:1)
我不确定,但您可能需要一些分组功能。
$case1 = Array
(
Array
(
'day_name' => 'Mon',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Wed',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Fri',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Sun',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
);
$case2 = Array
(
Array
(
'day_name' => 'Mon',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Tue',
'start_time' => '12:00 am',
'end_time' => '09:00 pm',
),
Array
(
'day_name' => 'Wed',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
);
$case3 = Array
(
Array
(
'day_name' => 'Mon',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Tue',
'start_time' => '01:00 pm',
'end_time' => '07:00 pm',
),
Array
(
'day_name' => 'Wed',
'start_time' => '12:00 am',
'end_time' => '09:00 pm',
),
Array
(
'day_name' => 'Thu',
'start_time' => '12:00 am',
'end_time' => '09:00 pm',
),
);
function group_same_interval($carry, $item)
{
$interval = $item['start_time'].' - '.$item['end_time'];
if (array_key_exists($interval, $carry))
{
$carry[$interval][] = $item['day_name'];
}
else
{
$carry[$interval] = array($item['day_name']);
}
return $carry;
}
print_r(array_reduce($case1, 'group_same_interval', array()));
print_r(array_reduce($case2, 'group_same_interval', array()));
print_r(array_reduce($case3, 'group_same_interval', array()));
结果如下
Array
(
[01:00 pm - 07:00 pm] => Array
(
[0] => Mon
[1] => Wed
[2] => Fri
[3] => Sun
)
)
Array
(
[01:00 pm - 07:00 pm] => Array
(
[0] => Mon
[1] => Wed
)
[12:00 am - 09:00 pm] => Array
(
[0] => Tue
)
)
Array
(
[01:00 pm - 07:00 pm] => Array
(
[0] => Mon
[1] => Tue
)
[12:00 am - 09:00 pm] => Array
(
[0] => Wed
[1] => Thu
)
)