[lang-ref] ( deep_copy ) ( php )
<?php
public function testDeepCopy(): void
{
// unserialize(serialize($items))
// array assignment copies by value in PHP
$itemsOriginal = [['A1', 'B1'], ['A2', 'B2']];
$itemsShallow1 = $itemsOriginal;
$itemsShallow2 = $itemsOriginal;
$items_deep = unserialize(serialize($itemsOriginal));
// value equality
$this->assertSame('A1', $itemsShallow1[0][0]);
$this->assertSame('B1', $itemsShallow1[0][1]);
$this->assertSame('A2', $itemsShallow1[1][0]);
$this->assertSame('B2', $itemsShallow1[1][1]);
$this->assertSame('A1', $itemsShallow2[0][0]);
$this->assertSame('B1', $itemsShallow2[0][1]);
$this->assertSame('A2', $itemsShallow2[1][0]);
$this->assertSame('B2', $itemsShallow2[1][1]);
$this->assertSame('A1', $items_deep[0][0]);
$this->assertSame('B1', $items_deep[0][1]);
$this->assertSame('A2', $items_deep[1][0]);
$this->assertSame('B2', $items_deep[1][1]);
$itemsOriginal[0] = ['A1-mod', 'B1-mod'];
$itemsOriginal[1][1] = 'B2-mod';
$this->assertSame('A1', $itemsShallow1[0][0]);
$this->assertSame('B1', $itemsShallow1[0][1]);
$this->assertSame('A2', $itemsShallow1[1][0]);
$this->assertSame('B2', $itemsShallow1[1][1]);
$this->assertSame('A1', $itemsShallow2[0][0]);
$this->assertSame('B1', $itemsShallow2[0][1]);
$this->assertSame('A2', $itemsShallow2[1][0]);
$this->assertSame('B2', $itemsShallow2[1][1]);
$this->assertSame('A1', $items_deep[0][0]);
$this->assertSame('B1', $items_deep[0][1]);
$this->assertSame('A2', $items_deep[1][0]);
$this->assertSame('B2', $items_deep[1][1]);
}