php array copy key value subarray

PHP Array Copy (7 Methods)

PHP array copy is an action to duplicate a PHP array (deep copy and shallow copy). This action can be done in many ways such as = operator, pointer, ArrayObject class, and user-defined function to copy an array with subarray in PHP. There are several built-in php functions to copy arrays in PHP too.

PHP deep copy and shallow copy of an array

A deep copy is an array clone with a unique reference and dedicated space in memory.

A shallow copy is an array clone with shared reference and memory space.

1. How to copy an array in PHP by = operator

In PHP, all variables except objects are assigned by the copy-on-write mechanism, while things are assigned by reference. This means that for the arrays with scalar values simply by = operand. This method is a deep copy.

$cars=array('fer'=>'Ferrari','ben'=>'Benz','bmw'=>'BMW');

$cars2=$cars;

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

You need to just assign them to other variable to get copy of that array.

2. By pointer

In the previous section, you have 2 different arrays with the same keys and values. But if you use a pointer (&) you will have one array with 2 names. This method is a shallow copy.

Let’s see how to make a shallow copy of an array in PHP.

$cars=array('fer'=>'Ferrari','ben'=>'Benz','bmw'=>'BMW');

$cars2=&$cars;

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

As the clone is a shallow copy, changing it affects the original because they both point to the same array residing in the same memory space. If you change a value in the array, the other array value will be changed.

$cars=array('fer'=>'Ferrari','ben'=>'Benz','bmw'=>'BMW');

$cars2=&$cars;

$cars['vol']='Volvo';

var_dump($cars2);

Output

array (size=4)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)

All you need is to assign the reference to a new variable. Think of a reference as a pointer to the exact memory location of the original array.

3. By ArrayObject class

Another way to copy the array is using ArrayObject class. The getArrayCopy() function of the ArrayObject class in PHP is used to create a copy of this ArrayObject. This function returns the copy of the array present in this ArrayObject.

$cars = new ArrayObject(['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW']);

$cars2 = $cars->getArrayCopy();

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

4. With serialize and unserialize

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

$cars2 = unserialize(serialize($cars));

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

5. Copy array with array_replace() function

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

$cars2 = array_replace([],$cars);

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

6. Clone with array_merge() function

This function works exactly like the array_replace() function.

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

$cars2 = array_merge([],$cars);

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

7. How to copy multidimensional array in PHP with user-defined function (an array and subarray)

A multidimensional array takes complexity to the next level. It is challenging in a way that it can include many levels of subarrays and varying data types. Here’s the most robust function to deep copy an array and subarray in PHP.

function clone_array($arr)
{
    $clone = [];
    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $clone[$k] = clone_array($v);
        }
        elseif (is_object($v)) {
            $clone[$k] = clone $v;
        }
        else {
            $clone[$k] = $v;
        }
    }
    return $clone;
}

$cars = ['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW'];

$cars2 = clone_array($cars);

var_dump($cars2);

Output

array (size=3)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

You may like

Shopping Cart