duanlujiaji10335 2016-11-10 05:34
浏览 27
已采纳

在PHP中更改现有的数组键值

My requirement is to show the list of dates in the array and the time available in that particular date.

I got set all the logic but struggling in one place, please look down about the issue.

I have an array Like below, what I need is to change the 'booked_from_time' to 'from_time' dynamically which ever I want.

How can I achieve this, Please help me to achieve the solution for it.

<!-- Array response -->

[2016-11-25] => Array
    (
        [0] => Array
            (
                [from_time] => 00:00:00
                [to_time] => 23:59:59
            )

    )

[2016-11-26] => Array
    (
        [0] => Array
            (
                [booked_from_time] => 2016-11-26 00:00:00
                [booked_to_time] => 2016-11-26 16:00:00
            )

        [1] => Array
            (
                [booked_from_time] => 2016-11-26 16:00:00
                [booked_to_time] => 2016-11-26 19:20:00
            )

        [2] => Array
            (
                [from_time] => 00:00:00
                [to_time] => 23:59:59
            )

    )

[2016-11-27] => Array
    (
        [0] => Array
            (
                [from_time] => 00:00:00
                [to_time] => 23:59:59
            )

    )
//My Php Code
function multipleBookingsInADay($tempList){
    foreach($tempList as $key => $dataValue){
        $previousFromDate;
        foreach($dataValue as $keyOne => $innerData){

            if(isset($innerData['booked_from_time'])){
                $tempStartTime = date('Y-m-d H:i:s',strtotime($key . " 00:00:00"));
                $tempFromTime = date('Y-m-d H:i:s',strtotime($key . " " . $innerData['booked_from_time']));
                if(empty($previousFromDate)){
                    $previousFromDate = date('Y-m-d H:i:s',strtotime($key . " 00:00:00"));
                }
                $tempList[$key][$keyOne]['booked_from_time'] = $previousFromDate;
                $tempList[$key][$keyOne]['booked_to_time'] = $tempFromTime;
                $previousFromDate = $tempFromTime;
                continue;
            }
            break;
        }
    }
    print_r($tempList);
}

Thanks in Advance.

展开全部

  • 写回答

1条回答 默认 最新

  • duanguo7021 2016-11-10 23:42
    关注

    I have fixed my issue with the help of below link.

    Replace array keys

    Thank You.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部