使用另一个数组中的值合并/覆盖一个数组中的值

时间:2017-08-02 08:38:20

标签: php arrays compare

我已经查看了其他类似的问题,但找不到与我试图做的相符的问题。

我有2个阵列,有不同的键,长度不同。我需要使用第二个值更新一个数组的值。第二个数组的长度可变,不会设置为特定的数字。

$times = [
        [ 'time' => '00:00', 'status' => 'unset' ],
        [ 'time' => '01:00', 'status' => 'unset' ],
        [ 'time' => '02:00', 'status' => 'unset' ],
        [ 'time' => '03:00', 'status' => 'unset' ],
        [ 'time' => '04:00', 'status' => 'unset' ],
        [ 'time' => '05:00', 'status' => 'unset' ],
        [ 'time' => '06:00', 'status' => 'unset' ],
        [ 'time' => '07:00', 'status' => 'unset' ],
        [ 'time' => '08:00', 'status' => 'unset' ],
        [ 'time' => '09:00', 'status' => 'unset' ],
        [ 'time' => '10:00', 'status' => 'unset' ],
        [ 'time' => '11:00', 'status' => 'unset' ],
        [ 'time' => '12:00', 'status' => 'unset' ],
        [ 'time' => '13:00', 'status' => 'unset' ],
        [ 'time' => '14:00', 'status' => 'unset' ],
        [ 'time' => '15:00', 'status' => 'unset' ],
        [ 'time' => '16:00', 'status' => 'unset' ],
        [ 'time' => '17:00', 'status' => 'unset' ],
        [ 'time' => '18:00', 'status' => 'unset' ],
        [ 'time' => '19:00', 'status' => 'unset' ],
        [ 'time' => '20:00', 'status' => 'unset' ],
        [ 'time' => '21:00', 'status' => 'unset' ],
        [ 'time' => '22:00', 'status' => 'unset' ],
        [ 'time' => '23:00', 'status' => 'unset' ],
    ];

$times是我需要更新值的数组,$times的索引总是为24(一天24小时)。另一个数组$slots看起来像这样(我为了简洁而将其修剪掉了)

$slots = [
        [
            'id'          => '104',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'available',
            'start_time'  => '2017-08-01 12:00:00',
            'end_time'    => '2017-08-01 13:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
        [
            'id'          => '105',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'available',
            'start_time'  => '2017-08-01 15:00:00',
            'end_time'    => '2017-08-01 16:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
        [
            'id'          => '106',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'unavailable',
            'start_time'  => '2017-08-01 17:00:00',
            'end_time'    => '2017-08-01 18:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
    ];

好的,我需要做的是,使用$times数组的status更新$slots数组,其中$slots start_time$times匹配{1}} time

为了让timestart_time匹配,我使用以下代码

foreach ( $slots as $slot ) {
            $slot_time = new DateTime( $slot[ 'start_time' ] );
            $slot_time = $slot_time->format( 'H:i' );}

3 个答案:

答案 0 :(得分:0)

你是正确的方向。你可以这样做;

$times_time = array_column($times, "time"); //only contain `time` values

foreach ( $slots as $slot ) {
    $slot_time = new DateTime( $slot[ 'start_time' ] );
    $slot_time = $slot_time->format( 'H:i' );
    $search_key = array_search($slot_time,$times_time);    
    if($search_key>0){ //search key
      $times[$search_key]['status']=$slot[ 'status' ];
    }
}
print_r($times);

Demo

答案 1 :(得分:0)

您将此代码添加到上一个foreach中 &符号&允许数组的值$t可修改

foreach ( $slots as $slot ) {
            $slot_time = new DateTime( $slot[ 'start_time' ] );
            $slot_time = $slot_time->format( 'H:i' );

            foreach ($times as &$t)
                if( $t['time']==$slot_time){
                    $t['status']=$slot['status'];
                }
        }

这是execution的输出,我用过print_r($times);

答案 2 :(得分:0)

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php


$times = [
        [ 'time' => '00:00', 'status' => 'unset' ],
        [ 'time' => '01:00', 'status' => 'unset' ],
        [ 'time' => '02:00', 'status' => 'unset' ],
        [ 'time' => '03:00', 'status' => 'unset' ],
        [ 'time' => '04:00', 'status' => 'unset' ],
        [ 'time' => '05:00', 'status' => 'unset' ],
        [ 'time' => '06:00', 'status' => 'unset' ],
        [ 'time' => '07:00', 'status' => 'unset' ],
        [ 'time' => '08:00', 'status' => 'unset' ],
        [ 'time' => '09:00', 'status' => 'unset' ],
        [ 'time' => '10:00', 'status' => 'unset' ],
        [ 'time' => '11:00', 'status' => 'unset' ],
        [ 'time' => '12:00', 'status' => 'unset' ],
        [ 'time' => '13:00', 'status' => 'unset' ],
        [ 'time' => '14:00', 'status' => 'unset' ],
        [ 'time' => '15:00', 'status' => 'unset' ],
        [ 'time' => '16:00', 'status' => 'unset' ],
        [ 'time' => '17:00', 'status' => 'unset' ],
        [ 'time' => '18:00', 'status' => 'unset' ],
        [ 'time' => '19:00', 'status' => 'unset' ],
        [ 'time' => '20:00', 'status' => 'unset' ],
        [ 'time' => '21:00', 'status' => 'unset' ],
        [ 'time' => '22:00', 'status' => 'unset' ],
        [ 'time' => '23:00', 'status' => 'unset' ],
    ];

$slots = [
        [
            'id'          => '104',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'available',
            'start_time'  => '2017-08-01 12:00:00',
            'end_time'    => '2017-08-01 13:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
        [
            'id'          => '105',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'available',
            'start_time'  => '2017-08-01 15:00:00',
            'end_time'    => '2017-08-01 16:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
        [
            'id'          => '106',
            't_id'        => '41',
            'u_id'        => null,
            'status'      => 'unavailable',
            'start_time'  => '2017-08-01 17:00:00',
            'end_time'    => '2017-08-01 18:00:00',
            'update_time' => '0000-00-00 00:00:00',
        ],
    ];

    $param = array();

    for($i=0;$i<count($times);$i++){
        for($j=0;$j<count($slots);$j++){
            if($times[$i]['time']==date('H:i',strtotime($slots[$j]['start_time']))){
                $times[$i]['start_time'] = date('H:i',strtotime($slots[$j]['start_time']));
                $param[$i]['time']=$times[$i]['time'];
                $param[$i]['status'] = $times[$i]['status'];
                $param[$i]['start_time'] = $times[$i]['start_time'];
            }

        }
    }
    print_r($param);
?>

</body>
</html>