search
HomeBackend DevelopmentPHP ProblemWhat are the ways to merge arrays in php

What are the ways to merge arrays in php

1. array_merge

Sample code:

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 6, 7, 8, 9, 10);
$result1 = array_merge($arr1, $arr2);
$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male");
$result2 = array_merge($arr3, $arr4);
echo "<pre class="brush:php;toolbar:false">";
var_dump($result1);
var_dump($result2);

Run the above code and output the result As shown in the figure below:

What are the ways to merge arrays in php

When ordinary arrays are merged, the second array will be placed behind the first array and returned after splicing.

Related recommendations: "PHP Introduction Tutorial"

But for an array of key-value pairs, if there are the same keys, then the second array will overwrite the first An array of values ​​corresponding to the same key.

2. Merge through " "

Sample code:

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 6, 7, 8, 9, 10);
$result1 = $arr1 + $arr2;
$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male", "id" => "411521");
$result2 = $arr3 + $arr4;
echo "<pre class="brush:php;toolbar:false">";
var_dump($result1);
var_dump($result2);

Run the above code, the output result is as shown below:

What are the ways to merge arrays in php

It can be seen that when using " " to merge two arrays, whether it is an ordinary array or a key-value pair array, as long as the subscript is the same or the key is the same, the former will overwrite it. the latter. This needs attention.

3. Connect two arrays

array_combine() function will get a new array, which consists of a set of submitted keys and corresponding values.

Sample code:

$arr1 = array("A","B","C","D"); 
$arr2 = array("paul","itbsl","Golang","PHP"); 
$result = array_combine($arr1,$arr2); 
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($result);

Run the above code, the output result is as shown below:

What are the ways to merge arrays in php

Note: If you need to merge the arrays into In the above form, the lengths of the two merged arrays must be equal, that is, count($arr1) == count($arr2), and cannot be empty.

4. Recursively append arrays

array_merge_recursive() function is the same as array_merge(). It can merge two or more arrays together to form a joint array. . The difference between the two is that the function will handle it differently when a key in an input array already exists in the result array. array_merge() will overwrite the previously existing key/value pairs and replace them with the key/value pairs in the current input array, while array_merge_recursive() will merge the two values ​​together to form a new array with the original keys. as an array name.

Sample code:

$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male");
$result2 = array_merge_recursive($arr3, $arr4);
echo "<pre class="brush:php;toolbar:false">";
var_dump($result2);

Run the above code, the output result is as shown below:

What are the ways to merge arrays in php

The above is the detailed content of What are the ways to merge arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.