php copying array elements by value, not by reference

前端 未结 6 1988
清歌不尽
清歌不尽 2020-12-06 05:48

I have the following code:

$data[\'x\'] = $this->x->getResults();  

$data[\'y\'] = $data[\'x\'];

//some code here to modify $data[\'y\']
//this cause         


        
6条回答
  •  独厮守ぢ
    2020-12-06 06:04

    You could use this function to copy multidimensional arrays containing objects.

     $val ) {
            if( is_array( $val ) ) {
                $result[$key] = arrayCopy( $val );
            } elseif ( is_object( $val ) ) {
                $result[$key] = clone $val;
            } else {
                $result[$key] = $val;
            }
        }
        return $result;
    }
    ?>
    

提交回复
热议问题