我遍历一个数组来比较当前和下一个键索引以检查值是否相同。 我正在制作两个数组:满足条件时的第一个数组 - 当前和下一个键索引的值相同;第二个数组是数组的其余元素。
但问题出在第二个数组中,我总是从第一个数组中得到一个元素。
[special_days_nested_array] => Array
(
[0] => Array
(
[day_name] => Tue
[start_time] => 05:00 am
[end_time] => 08:00 pm
)
[1] => Array
(
[day_name] => Fri
[start_time] => 06:00 am
[end_time] => 10:00 pm
)
[2] => Array
(
[day_name] => Sat
[start_time] => 12:00 am
[end_time] => 08:54 pm
)
[3] => Array
(
[day_name] => Sun
[start_time] => 12:00 am
[end_time] => 08:54 pm
)
)
这是我用来比较相同值的数组。
$j = 0;
foreach($data_set->special_days_nested_array as $key => $value){
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[$key]['day_name']]['day_name'] = $data_set->special_days_nested_array[$key]['day_name'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$key]['day_name']]['start_time'] = $data_set->special_days_nested_array[$key]['start_time'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$key]['day_name']]['end_time'] = $data_set->special_days_nested_array[$key]['end_time'];
}
$j++;
}
这里我遍历上面的数组并比较当前和下一个元素。
这是我成功比较的数组。
[special_days_same_time_array] => Array
(
[0] => Sat
[1] => Sun
)
[special_timings_same_time] => 12:00 am - 08:54 pm
这是我从else条件得到的数组:
[special_time_different_time_array] => Array
(
[Tue] => Array
(
[day_name] => Tue
[start_time] => 05:00 am
[end_time] => 08:00 pm
)
[Fri] => Array
(
[day_name] => Fri
[start_time] => 06:00 am
[end_time] => 10:00 pm
)
[Sun] => Array
(
[day_name] => Sun
[start_time] => 12:00 am
[end_time] => 08:54 pm
)
)
在这个数组中,前两个元素是正确的,但我得到的第三个元素是不正确的。
我无法弄清楚确切的问题。
答案 0 :(得分:0)
由于循环实现不正确,您的数组绑定元素有问题。那是因为对于最后一个元素,没有j+1
元素,因此您将始终输入条件的其他分支。
您还可以将常规for
语法与foreach
语法混合使用。我建议你在这种情况下使用简单明了的for
运算符:
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]['day_name']]['day_name'] = $data_set->special_days_nested_array[$j]['day_name'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$j]['day_name']]['start_time'] = $data_set->special_days_nested_array[$j]['start_time'];
$data_set->special_time_different_time_array[$data_set->special_days_nested_array[$j]['day_name']]['end_time'] = $data_set->special_days_nested_array[$j]['end_time'];
}
}