PHP array_replace without creating keys

前端 未结 5 1687
不思量自难忘°
不思量自难忘° 2021-02-05 11:25

I am trying to overwrite the elements of one array with values from another – without creating additional elements in the process.

For example:

         


        
5条回答
  •  面向向阳花
    2021-02-05 12:09

    I can't think of a built in method for that, however, it would be trivial with a loop and array_key_exists.

    foreach( $replace as $k => $v )
    {
       if ( array_key_exists( $k, $base ) )
          $base[ $k ] = $v;
    }
    

提交回复
热议问题