Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Array union by key (+)

Combine two arrays by key, taking the left value whenever both arrays contain the same key.

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

Syntax

$preferred + $fallback

Combine two arrays by key, taking the left value whenever both arrays contain the same key. Integer keys participate in exactly the same rule as string keys. This suits filling missing settings from defaults, but combining two ordinary lists can discard values at overlapping positions. A present null value still wins.

$preferred + $fallback
{"0":"A","theme":null,"1":"C"}

Examples

<?php
$actual = [0=>'A','theme'=>null];
$defaults = [0=>'B',1=>'C','theme'=>'light'];
echo json_encode($actual + $defaults);

Result

{"0":"A","theme":null,"1":"C"}

Sources