Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_merge_recursive()

Merge nested arrays recursively.

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

Syntax

array_merge_recursive(array ...$arrays): array

Merge nested arrays recursively. When a string key occurs in more than one input, its values are combined into an array, even if both values were scalars. Numeric keys append values. A field that was a single string can therefore become a list after configuration sources are combined.

Signature

array_merge_recursive(array ...$arrays): array

Example output

{"tag":["base","new"],"opts":{"format":"txt","width":40}}

Examples

<?php
$base = ['tag'=>'base','opts'=>['format'=>'txt']];
$extra = ['tag'=>'new','opts'=>['width'=>40]];
echo json_encode(array_merge_recursive($base, $extra));

Result

{"tag":["base","new"],"opts":{"format":"txt","width":40}}

Sources