Replace values in first associative array ($a1) with values in second array ($a2) with missing keys in PHP

Description

The following code shows how to replace values in first associative array ($a1) with values in second array ($a2) with missing keys.

Example


//from w w w .j a  v a  2 s  .  co  m
//If a key from array1 exists in array2, and if the key only exists in array1

<?php
    $a1=array("a"=>"red","b"=>"green");
    $a2=array("a"=>"orange","HTML");
    print_r(array_replace($a1,$a2));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone