PHP array_replace_recursive() Function

Definition

The array_replace_recursive() function replaces the values of the first array with the values from following arrays recursively.

Syntax

PHP array_replace_recursive() Function has the following syntax.

array_replace_recursive(array1,array2,array3...)

Parameter

ParameterIs RequiredDescription
array1Required.Array to be replaced
array2Optional.Array to replace array1
array3,...Optional.Array to replace array before array3(array1,array2)

Example 1

Replaces the values of the first array with the values from following arrays recursively


<?php// w  ww .j  av  a2s .c o m
    $a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
    $a2=array("a"=>array("B"),"b"=>array("HTML"));
    print_r(array_replace_recursive($a1,$a2));
?>

The code above generates the following result.

Example 2

Multiple arrays:


<?php//  w ww .  ja v a2 s .c o m
    $a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
    $a2=array("a"=>array("B"),"b"=>array("HTML"));
    $a3=array("a"=>array("C"),"b"=>array("CSS"));
    print_r(array_replace_recursive($a1,$a2,$a3));
?>

The code above generates the following result.

Example 3

Differences between array_replace() and array_replace_recursive():


<?php/*from  ww w .j  a v  a  2 s.  c  o m*/
    $a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
    $a2=array("a"=>array("B"),"b"=>array("HTML"));
    
    $result=array_replace_recursive($a1,$a2);
    print_r($result);
    
    $result=array_replace($a1,$a2);
    print_r($result);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions