Table of Contents
1、“+”运算符
2、array array_merge ( array array1 [, array array2 [, array ...]] )
3、array array_merge_recursive ( array array1 [, array ...] )
Home php教程 php手册 三种PHP合并数组的方法异同

三种PHP合并数组的方法异同

Jun 13, 2016 am 09:38 AM
array_merge Array merge

1、“+”运算符

规则:当两个数组的键名是数字键名或者字符串键名可以直接 +,$c = $a + $b,在$a后追加($b在$a中不存在的键名)键名和值。

注意:

  1. 不覆盖,只是追加不存在的键名和对应的值。
  2. 键名不重新索引。
  3. 无论是全部数字键名还是混合,都只是追加键名和值,如果键名相同则不进行追加,即把最先出现的值作为最终结果返回。
<?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

数字键名:

<?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

字符键名:

<?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 ...]] )

规则:array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。 如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。 

注意:

  1. 数字索引,不会覆盖,值合并后,键名会连续方式重新索引
  2. 字符串键名,则该键名后面的值将覆盖前一个值
<?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

数字键名:

<?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

字符键名:

<?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() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 

如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。

然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。 

注意:规则跟array_merge基本相同,只是在处理相同字符键名的时候,采用递归追加。

<?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

数字键名:

<?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

字符键名:

<?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
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 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)

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

PHP Warning: array_merge(): Argument solution PHP Warning: array_merge(): Argument solution Jun 22, 2023 pm 11:54 PM

Solution to PHPWarning:array_merge():Argument In PHP development, we often encounter the PHPWarning:array_merge():Argument error message. What does this mean? PHPWarning:array_merge():Argument error is caused because one or more parameters are not of array type. array_merge() function combination

See all articles