PHP recursively replaces the value function of the first array with the value of the subsequent array array_replace_recursive ()

Source: Internet
Author: User

Instance

Recursively replaces the value of the first array ($a 1) with the value of the second array ($a 2):

<?php$a1=array ("A" =>array ("Red"), "B" =>array ("green", "blue"), $a 2=array ("a" =>array ("Yellow"), "b" = >array ("Black"));p Rint_r (array_replace_recursive ($a 1, $a 2));? >

Definition and usage

The array_replace_recursive () function recursively replaces the value of the first array with the value of the subsequent array.

Tip: You can pass an array, or multiple arrays, to a function.

If a key exists in the first array array1 also exists in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2. If a key exists only in the first array array1, it will remain unchanged. If a key exists in the second array array2, but does not exist in the first array array1, the element is created in the first array array1. If more than one substitution array is passed, they are processed sequentially, and the values of the arrays are overwritten by the value of the previous array.

Note: If you do not specify a key for each array, the function behaves the same as the Array_replace () function.

Grammar

Array_replace_recursive (Array1,array2,array3 ...)
parameters description
array1 required. Specifies an array.
array2 optional. Specifies an array of values to replace Array1.
optional. Specify multiple to replace Array1 and array2, ... An array of values. The value of the subsequent array overrides the value of the previous array.

Technical details

return value: Returns the replaced array, or NULL if an error occurs.
PHP version: 5.3.0+

More examples

Example 1

Multiple arrays:

<?php$a1=array ("A" =>array ("Red"), "B" =>array ("green", "Blue")), $a 2=array ("a" =>array ("Yellow"), "b" = >array ("Black")), $a 3=array ("a" =>array ("Orange"), "B" =>array ("Burgundy"));p Rint_r (array_replace_ Recursive ($a 1, $a 2, $a 3));? >

Example 2

The difference between array_replace () and array_replace_recursive ():

<?php$a1=array ("a" =>array ("Red"), "B" =>array ("green", "Blue"),); $ A2=array ("A" =>array ("Yellow"), "B" =>array ("Black"), $result =array_replace_recursive ($a 1, $a 2);p Rint_r ($ result); $result =array_replace ($a 1, $a 2);p Rint_r ($result);? 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.