R4R Team working to provide free of cost for experienced and freshers

Tutorials | Examples|Interview Questions and Answers|Java Tutorials| C Tutorials | C++ Tutorials | Spring Tutorials| Hibernate Tutorials | JSP Tutorials | Servelets Tutorials| C# Tutorials | PHP Tutorials etc , Basics | Download Software | Data Types | Variables | Constant | Storage & Classes | Operators | Decisions Making | Loops | Functions| Numbers | Arrays | Pointers | String | Datastructure | Expressions | Preprocessor Statement | I/O | Exception Handling | OOPs Tutorial | Classes and object |C++ Inheritance Overloading | Polymorphism | Abstraction | Encapsulation | Interfaces | Multithreading etc

PHP Programing language

array_replace() Function in PHP
Previous Home Next

array_replace()

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

Syntax:
array_replace(array1,array2,array3...)

In above syntax, "array1" specifies an array, "array2" specifies an array which will replace the values of array1 and "array3,..." specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones.

Example:

<?php
$arr1=array("a"=>"Apple","b"=>"Mango");
$arr2=array("a"=>"Banana","Orange");
print_r(array_replace($arr1,$arr2));
?>

Output:

Array ( [a] => Banana [b] => Mango [0] => Orange )

array_replace_recursive()

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

Syntax:
array_replace_recursive(array1,array2,array3...)

In above syntax, "array1" specifies an array, "array2" specifies an array which will replace the values of array1 and "array3,..." specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones.

Example:

<?php
$arr1=array("a"=>array("Apple"),"b"=>array("Mango","Banana"));
$arr2=array("a"=>array("Orange"),"b"=>array("Guava"));
$arr3=array("a"=>array("Papaya"),"b"=>array("Pomigranate"));
print_r(array_replace_recursive($arr1,$arr2,$arr3));
?>

Output:

Array ( [a] => Array ( [0] => Papaya ) [b] => Array ( [0] => Pomigranate [1] => Banana ) )

Previous Home Next