Table of Contents
1. "+" operator
2. array array_merge ( array array1 [, array array2 [, array ...]] )
3. array array_merge_recursive ( array array1 [, array ...] )
Home Backend Development PHP Tutorial Similarities and Differences in Three PHP Methods of Merging Arrays_PHP Tutorial

Similarities and Differences in Three PHP Methods of Merging Arrays_PHP Tutorial

Jul 13, 2016 am 10:33 AM
array_merge Array merge

1. "+" operator

Rule: When the key names of the two arrays are numeric key names or string key names, you can directly +, $c = $a + $b, append after $a ($b is a key that does not exist in $a name) key name and value.

Note:

  1. does not overwrite, just appends the non-existing key name and corresponding value.
  2. Key names are not re-indexed.
  3. Whether it is all numeric key names or mixed, only the key name and value are appended. If the key names are the same, no appending is performed, that is, the first appearing value is returned as the final result.
<?php
$fruit_1 = array( 'apple', 'banana' );
$fruit_2 = array( 'orange', 'lemon' );
$fruit = $fruit_1 + $fruit_2;
var_dump($fruit);
 
// output:
// array(2) { [0]=> string(5) "apple" [1]=> string(6) "banana" }
?>
Copy after login

Number key name:

<?php
$a = array( 66=>'a' );
$b = array( 60=>'u', 66=>'c' );
$c = $a + $b;
var_dump($c);
 
// output:
// array(2) { [66]=> string(1) "a" [60]=> string(1) "u" }
?>
Copy after login

Character key name:

<?php
$a = array( 1=>'a', 2=>'b', 'c'=>'c', 'd'=>'d' );
$b = array( 1=>'u', 3=>'v', 'c'=>'w', 'd'=>'x', 'y'=>'y', 60=>'z' );
$c = $a + $b;
var_dump($c);
 
// output:
// array(7) { [1]=> string(1) "a" [2]=> string(1) "b" ["c"]=> string(1) "c" ["d"]=> string(1) "d" [3]=> string(1) "v" ["y"]=> string(1) "y" [60]=> string(1) "z" }
?>
Copy after login

2. array array_merge ( array array1 [, array array2 [, array ...]] )

Rule: array_merge() merges the cells of one or more arrays, and the values ​​in one array are appended to the previous array. Returns the resulting array. If the input array has the same string key name, the value after the key name will overwrite the previous value. However, if the array contains numeric keys, the subsequent values ​​will not overwrite the original values, but will be appended to them. If only an array is given and the array is numerically indexed, the keys are re-indexed consecutively.

Note:

  1. Numeric index will not be overwritten. After the values ​​are merged, the key names will be re-indexed in a continuous manner
  2. String key name, the value after the key name will overwrite the previous value
<?php
$a = array( 'a' );
$b = array( 'u' );
$c = array_merge($a, $b);
var_dump($c);
 
// output:
// array(2) { [0]=> string(1) "a" [1]=> string(1) "u" }
?>
Copy after login

Number key name:

<?php
$a = array( 66=>'a' );
$b = array( 60=>'u', 66=>'c' );
$c = array_merge($a, $b);
var_dump($c);
 
// output:
// array(3) { [0]=> string(1) "a" [1]=> string(1) "u" [2]=> string(1) "c" }
?>
Copy after login

Character key name:

<?php
$a = array( 1=>'a', 2=>'b', 'c'=>'c', 'd'=>'d' );
$b = array( 1=>'u', 3=>'v', 'c'=>'w', 'd'=>'x', 'y'=>'y', 60=>'z' );
$c = array_merge($a, $b);
var_dump($c);
 
// output:
// array(8) { [0]=> string(1) "a" [1]=> string(1) "b" ["c"]=> string(1) "w" ["d"]=> string(1) "x" [2]=> string(1) "u" [3]=> string(1) "v" ["y"]=> string(1) "y" [4]=> string(1) "z" }
?>
Copy after login

3. 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, the values ​​will be merged into an array, which will go on recursively, so if a value itself is an array, this function will put it according to the corresponding entry. It is merged 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.

Note: The rules are basically the same as array_merge, except that recursive append is used when processing the same character key name.

<?php
$a = array( 'a' );
$b = array( 'u' );
$c = array_merge_recursive($a, $b);
var_dump($c);
 
// output:
// array(2) { [0]=> string(1) "a" [1]=> string(1) "u" }
?>
Copy after login

Number key name:

<?php
$a = array( 66=>'a' );
$b = array( 60=>'u', 66=>'c' );
$c = array_merge_recursive($a, $b);
var_dump($c);
 
// output:
// array(3) { [0]=> string(1) "a" [1]=> string(1) "u" [2]=> string(1) "c" }
?>
Copy after login

Character key name:

<?php
$a = array( 1=>'a', 2=>'b', 'c'=>'c', 'd'=>'d' );
$b = array( 1=>'u', 3=>'v', 'c'=>'w', 'd'=>'x', 'y'=>'y', 60=>'z' );
$c = array_merge_recursive($a, $b);
var_dump($c);
 
// output:
// array(8) { [0]=> string(1) "a" [1]=> string(1) "b" ["c"]=> array(2) { [0]=> string(1) "c" [1]=> string(1) "w" } ["d"]=> array(2) { [0]=> string(1) "d" [1]=> string(1) "x" } [2]=> string(1) "u" [3]=> string(1) "v" ["y"]=> string(1) "y" [4]=> string(1) "z" }
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752522.htmlTechArticle1. "+" operator rule: When the key names of the two arrays are numeric key names or string keys The name can be directly +, $c = $a + $b, append (the key name of $b does not exist in $a) key name and value after $a. ...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to deal with empty arrays after merging PHP arrays? How to deal with empty arrays after merging PHP arrays? Apr 28, 2024 pm 01:51 PM

When using array_merge() in PHP to merge arrays, it will produce confusing results if it contains empty strings or empty arrays. Solution: 1. Use array_filter() to filter null values. 2. For cases containing empty arrays, use the recursive merge function array_merge_recursive_distinct() to maintain a consistent array structure.

How does PHP array merging efficiency compare? How does PHP array merging efficiency compare? Apr 28, 2024 am 11:09 AM

Comparison of PHP array merging efficiency: The time complexity of the three methods Array_merge(), + operator and Array_replace() is all O(n), which means that the merging time is proportional to the number of array elements. The space complexity of these three methods is also O(n), which means that the memory usage is proportional to the number of array elements. Measured results show that Array_merge() and + operator are faster than Array_replace() when merging large arrays.

How to consider time complexity when merging PHP arrays? How to consider time complexity when merging PHP arrays? Apr 28, 2024 pm 02:18 PM

For array merging in PHP, the time complexity depends on the algorithm: O(m+n) for array_merge() and + operator, where m and n are the array sizes. Loop merging is also O(m+n). Choose the appropriate method based on factors such as array size and availability, and consider performance needs to optimize your application.

Merge operation of arrays in PHP8.0: array_merge Merge operation of arrays in PHP8.0: array_merge May 14, 2023 am 08:52 AM

In PHP8.0 version, the array merging operation has been improved. This improvement mainly targets the merging operation of array data types. In previous versions, the array merging operations provided by PHP were implemented using the "+" symbol. However, there are some problems with this approach. If two arrays contain the same keys, the key values ​​in the second array will overwrite the key values ​​in the first array. If you need to merge the two arrays together, you need to use the array_merge() function skillfully. . Now, in PHP

How to deal with duplicate elements when merging PHP arrays? How to deal with duplicate elements when merging PHP arrays? Apr 28, 2024 pm 10:42 PM

When merging arrays in PHP, you can choose the following method to deal with duplicate elements: use array_merge() combined with array_unique() to remove duplicate elements. Use array_replace() to overwrite duplicate elements without changing the original array. Use array_diff() to remove elements from one array that are not in another array.

PHP 5.5 function analysis: How to use the array_reduce function to combine array elements into one value PHP 5.5 function analysis: How to use the array_reduce function to combine array elements into one value Jul 30, 2023 pm 05:18 PM

PHP5.5 function analysis: How to use the array_reduce function to combine array elements into one value. In PHP programming, we often need to process arrays, and sometimes we need to combine array elements into one value. At this time, we can use the array_reduce function introduced in PHP5.5 version to implement this function. This article will introduce the use of array_reduce function in detail and provide corresponding code examples. The array_reduce function is a

How to merge and split PHP arrays How to merge and split PHP arrays Sep 05, 2023 am 08:47 AM

PHP array is a very commonly used data structure, and the merging and splitting operations of arrays are often involved in development. This article will introduce how to use PHP language to implement these two operations, and attach corresponding code examples. 1. Merge arrays The operation of merging arrays can be implemented using the array_merge() function. This function accepts multiple arrays as arguments and merges them into a new array. Code example: $array1=["apple","ba

Use the PHP function 'array_merge' to merge multiple arrays into one array Use the PHP function 'array_merge' to merge multiple arrays into one array Jul 24, 2023 am 11:49 AM

Use the PHP function "array_merge" to merge multiple arrays into one array. In PHP development, sometimes we need to merge multiple arrays into one large array to process data more conveniently. In order to achieve this function, PHP provides a powerful function "array_merge". The array_merge function is very simple to use. It accepts multiple arrays as parameters and merges them into a new array. Here is the basic syntax for using the function: array_merg

See all articles