Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_replace()

Overlay later arrays on the first array by key, including integer keys.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_replace(array $array, array ...$replacements): array

Overlay later arrays on the first array by key, including integer keys. A later value replaces the entire earlier value at that key rather than recursively combining its contents. Keys found only in the later arrays are added. This is useful when replacing complete configuration fields.

Signature

array_replace(array $array, array ...$replacements): array

Example output

{"7":"new","view":{"size":5}}

Examples

<?php
$base = [7=>'old', 'view'=>['page'=>1,'size'=>20]];
echo json_encode(array_replace($base, [7=>'new', 'view'=>['size'=>5]]));

Result

{"7":"new","view":{"size":5}}

Sources