English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article illustrates the usage of the PHP array_merge function. Shared for everyone's reference, as follows:
Merging arrays involves appending one array to another, primarily achieved through the array_merge() function.
The syntax is as follows:
array array_merge(array array1,array array2[,array...]);
Note:
When merging, if the input array contains duplicate string key names, the later values will overwrite the earlier ones; if the array contains numeric key names, the later values will not overwrite the original ones but will be appended to the end.
For example:}}
$arr1=> array("Book" => "PHP Standard Tutorial"); $arr2=> array("Network Class", "Pricing", "Book" => "PHP from Beginner to Expert", "PHP" =>"95", "Element"); $res = array_merge($arr1$arr2); print_r($res);
The running result is:
Array ( [Book] => PHP from Beginner to Expert [0] => Network Class [1=> Pricing [PHP] => 95 [2=> Element )
Note:
The array_merge_recursive() function is similar to the array_merge() function, and is also used to merge arrays, but it can retain elements with the same character key values that appear in both arrays.
For example, as in the above example:
$arr1=> array("Book" => "PHP Standard Tutorial"); $arr2=> array("Network Class", "Pricing", "Book" => "PHP from Beginner to Expert", "PHP" =>"95", "Element"); $res = array_merge_recursive($arr1$arr2); print_r($res);
The running result is:
Array ( [Book] => Array ( [0] => PHP Standard Tutorial [1=> PHP from Beginner to Expert ) [0] => Network Class [1=> Pricing [PHP] => 95 [2=> Element )
Readers who are interested in more PHP-related content can check out the special topics on this site: 'Complete Collection of PHP Array (Array) Operation Techniques', 'Summary of PHP String (string) Usage', 'PHP Data Structures and Algorithms Tutorial', 'Summary of PHP Program Design Algorithms', 'Summary of PHP Mathematical Operation Techniques', and 'Summary of Common Database Operation Techniques in PHP'.
I hope this article will be helpful to everyone in PHP program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.