Home >Backend Development >PHP Tutorial >Explore different approaches to PHP array deep copying: performance, pros and cons
In PHP, you can use the array_merge_recursive(), json_encode()/json_decode() and clone methods to copy an array. array_merge_recursive() recursively merges nested arrays, but is slower; json_encode()/json_decode() is faster, but consumes memory; clone is the fastest, but only works on objects (including arrays).
Explore different methods of array deep copying in PHP: performance, advantages and disadvantages
Introduction
In PHP, copying an array is a common operation. However, the default assignment operator does not create a copy of the array, but creates a reference to the original array. In some cases, this can lead to unintended consequences. Therefore, it is important to understand the different methods of deep copying arrays in PHP and their pros and cons.
Method 1: array_merge_recursive()
array_merge_recursive()
The function will merge multiple arrays into a new array and merge them recursively. Any nested array. It can be used to create a deep copy of an array.
$original = ['key1' => 'value1', 'key2' => ['subkey1' => 'subvalue1']]; $copy = array_merge_recursive([], $original);
Advantages:
Disadvantages:
Method 2: json_encode() and json_decode()
json_encode()
The function converts a PHP variable to a JSON string , json_decode()
function converts JSON string into PHP variable. We can use these functions to create a deep copy of an array.
$original = ['key1' => 'value1', 'key2' => ['subkey1' => 'subvalue1']]; $copy = json_decode(json_encode($original), true);
Advantages:
array_merge_recursive()
. Disadvantages:
Method 3: Using clone
Cloning objects also works with arrays as it creates a completely separate copy of the original array copy.
$original = ['key1' => 'value1', 'key2' => ['subkey1' => 'subvalue1']]; $copy = clone $original;
Advantages:
Disadvantages:
Practical Case
The following is a practical case that demonstrates how to use deep copy of a PHP array:
<?php // 创建一个包含嵌套数组的原始数组 $original = [ 'name' => 'John', 'age' => 25, 'address' => [ 'street' => 'Main Street', 'city' => 'Anytown' ] ]; // 创建使用不同方法的深度副本 $copy1 = array_merge_recursive([], $original); $copy2 = json_decode(json_encode($original), true); $copy3 = clone $original; // 验证副本与原始数组是否不同 var_dump($copy1 !== $original); // 输出:true var_dump($copy2 !== $original); // 输出:true var_dump($copy3 !== $original); // 输出:true
In the above example , we create a primitive array containing nested arrays. We then created three deep copies using array_merge_recursive()
, json_encode()/json_decode()
and clone
. Finally, we use var_dump()
to verify that the copies are different from the original array, and the result is true
, indicating that these copies are independent instances of the original array.
The above is the detailed content of Explore different approaches to PHP array deep copying: performance, pros and cons. For more information, please follow other related articles on the PHP Chinese website!