duanlujiaji10335 2016-11-10 13:34
浏览 26
已采纳

在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-11 07:42
    关注

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

    Replace array keys

    Thank You.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备