PHP array_merge_recursive Array Merge _php instance

Source: Internet
Author: User

In PHP, the array_merge_recursive function is used to combine cells of one or more arrays, and the values in an array are appended to the previous array, returning as an array of results. This article will detail the use of array_merge_recursive functions and examples.

Let's first introduce the basic syntax of the array_merge_recursive function:

Array array_merge_recursive (array $array 1 [, Array $ ...])

Array_merge_recursive () Merges the cells of one or more arrays, and the values in one array are appended to the previous array. Returns an array as the result.

If the input array has the same string key name, the values are merged into an array, which is recursive, so if a value itself is an array, this function merges it into another array according to the corresponding entry. However, if the array has the same array key name, the latter value will not overwrite the original value, but append to the back.

Parameter introduction:

Parameters Description
Array1 Necessary. The initial array to merge.
Array2 Optional. A list of array variables for recursive merging.

return value

An array of results in which the values are merged from the attached parameters.

Attention:

The difference between this function and the Array_merge () function is to handle two or more array elements with the same key name. Array_merge_recursive () does not overwrite the key name, but instead recursively composes the values of multiple identical key names into an array.

If you enter only an array to the array_merge_recursive () function, and the result is the same as Array_merge (), the function returns a new array with the integer key name, whose key name is indexed in 0.
Instance:

<?php
$ar 1 = Array ("COLOR" => Array ("Favorite" => "Red"), 5);
 $ar 2 = Array ("COLOR" => Array ("Favorite" => "green", "Blue"));
 $result = array_merge_recursive ($ar 1, $ar 2);
 Print_r ($result);
 ? > 

Results:

Array
(
  [color] => Array ([
      favorite] => array
        ([
          0] => red
          [1] => green< c15/>)      
        [0] => Blue
    )  
  [0] => 5
  [1] =>


Thank you for reading, I hope to help you, thank you for your support for this site!

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.