douduan2272 2015-03-31 06:57
浏览 112
已采纳

遍历数组以比较当前和下一个键索引

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.

  • 写回答

1条回答 默认 最新

  • dongxiangchan0743 2015-03-31 07:16
    关注

    You have a problem with bound element of your array because of incorrect loop implementation. That is because for the last element there is no j+1th element so you will always enter else branch of condition.

    Also you mix regular for syntax with foreach syntax. I would recommend you to use simple and straightforward for operator for this case:

        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'];
            }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源