Home > Backend Development > PHP Tutorial > php array_merge_recursive array merge

php array_merge_recursive array merge

墨辰丷
Release: 2023-03-28 16:00:02
Original
3358 people have browsed it

This article mainly introduces the relevant information about php array_merge_recursive merging two or more arrays into one array. Friends who need it can refer to

The array_merge_recursive function in php is used to merge one or more arrays. The cells are combined, the values ​​in one array are appended to the previous array, and the resulting array is returned. This article will introduce in detail the usage and examples of the array_merge_recursive function.

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

array array_merge_recursive ( array $array1 [, array $... ] )

array_merge_recursive() Merges the cells of one or more arrays, with the values ​​in one array appended to the previous array. Returns the resulting array.

If the input arrays have the same string key name, these values ​​​​will be merged into an array, which will go on recursively, so if a value itself is an array, this function will follow the corresponding entries into another array. However, if the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to it.

Parameter introduction:


Parameter Description
array1 Required. The initial array to merge.
array2 Optional. List of array variables for recursive merging.

Return Value

A result array whose values ​​are combined from the appended parameters.

Note:

The difference between this function and the array_merge() function is when two or more array elements have the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values ​​with the same key name into an array.

If you simply input an array to the array_merge_recursive() function, the result is the same as array_merge(), the function will return a new array with integer keys, with keys starting at 0 for re-indexing.
Example:

<?php
$ar1 = array( "color" => array( "favorite" => "red" ), 5 );
 $ar2 = array( 10 , "color" => array( "favorite" => "green" , "blue" ));
 $result = array_merge_recursive ( $ar1 , $ar2 );
 print_r ( $result );
 ?>
Copy after login

Result:

Array
(
  [color] => Array
    (
      [favorite] => Array
        (
          [0] => red
          [1] => green
        )      
        [0] => blue
    )  
  [0] => 5
  [1] => 10
)
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.


Related recommendations:

How to set the file size using the readfile() function in php

Analysis of the difference between new self() and new static() in PHP

##PHP implements file lock and process lock

The above is the detailed content of php array_merge_recursive array merge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template