I am traversing an array to compare the current and next key index to check if the values are same. I am making two arrays: First array when the condition is met -- value of current and next key index is same; second array is the remaining elements of an array.
But the problem is in the second array, I always get one element from the first array.
[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
)
)
This is the array which I use for comparing the same value.
$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++;
}
Here i am traversing over the above array and comparing the current and next elements.
This is the array that I get in successful comparison.
[special_days_same_time_array] => Array
(
[0] => Sat
[1] => Sun
)
[special_timings_same_time] => 12:00 am - 08:54 pm
This is the array that I get from the else condition:
[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
)
)
In this array, the first two elements are correct, but the third element I get is not correct.
I am not able to figure out the exact problem.