Home > Backend Development > PHP Tutorial > The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

WBOY
Release: 2024-05-01 12:30:01
Original
938 people have browsed it

Methods for deep copying arrays in PHP include: using json_decode and json_encode for JSON encoding and decoding. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

The Art of PHP Array Deep Copy: Using Different Methods to Achieve Perfect Copy

In PHP, array is a powerful data Structures used to store and manage related data. Sometimes we need to create a copy of an array to perform operations or to avoid accidental modifications to its original data. This is where deep copy comes into play: it creates a new array containing all the elements of the original array, including nested arrays and objects.

Method 1: json_decode and json_encode

<?php
$originalArray = [
    'name' => 'John Doe',
    'age' => 30,
    'hobbies' => ['reading', 'hiking', 'coding']
];

// JSON 编码原始数组
$jsonEncoded = json_encode($originalArray);

// JSON 解码 JSON 编码的字符串
$deepCopy = json_decode($jsonEncoded, true);

print_r($deepCopy);
?>
Copy after login

Method 2: array_map and clone

<?php
$originalArray = [
    'name' => 'Jane Smith',
    'age' => 25,
    'address' => ['street' => 'Main Street', 'number' => 123]
];

// 复制数组键及其值的深度副本
$deepCopy = array_map(function($value) {
    return is_array($value) ? array_map('clone', $value) : clone $value;
}, $originalArray);

print_r($deepCopy);
?>
Copy after login

Method 3: serialize and unserialize

<?php
$originalArray = [
    'product' => 'Book',
    'quantity' => 5,
    'details' => ['author' => 'John Smith', 'publisher' => 'ACME Press']
];

// 序列化原始数组
$serialized = serialize($originalArray);

// 反序列化序列化字符串
$deepCopy = unserialize($serialized);

print_r($deepCopy);
?>
Copy after login

Practical Case: Shopping Cart Manager

Consider a shopping cart manager that manages the items in the user's shopping basket. To avoid modifying the original basket, we need to create a copy of it to perform operations such as calculating the total price or removing items. Deep copying ensures that we have an independent copy that does not affect the original basket.

<?php
// 原始购物篮数组
$originalCart = [
    ['product' => 'Book', 'quantity' => 3],
    ['product' => 'Pen', 'quantity' => 2],
    ['product' => 'Charger', 'quantity' => 1]
];

// 使用方法 1 创建购物篮副本
$deepCopy = json_decode(json_encode($originalCart), true);

// 在副本中移除一个商品
unset($deepCopy[1]);

// 输出更新后的购物篮副本
print_r($deepCopy);
?>
Copy after login

The above is the detailed content of The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template