In php, The array_merge merge array is described in detail. The array_merge Array

Source: Internet
Author: User

In php, The array_merge merge array is described in detail. The array_merge Array

If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.

Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed.

The Code is as follows:

<? Php
$ A = array (3 => "Horse", 4 => "Dog ");
Print_r (array_merge ($ ));
?>



Combine the elements of one or more arrays. The values of an array are appended to the values of the previous array. Returns an array of results.

If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.

If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.

Example:

The Code is as follows:
$ Face = array ("J", "Q", "K", "");
$ Numbered = array ("2", "3", "4", "5", "6", "7", "8", "9 ");
$ Cards = array_merge ($ face, $ numbered );
Shuffle ($ cards );
Print_r ($ cards );



This will return the following results,

 

The Code is as follows:
Array ([0] => A [1] => 4 [2] => 9 [3] => 3 [4] => K [5] => 7 [6] => 5 [7] => Q [8] => 6 [9] => 8 [10] => 2 [11] => J)

Recursive append Array
The array_merge_recursive () function is the same as the array_merge () function. Two or more numbers can be combined to form a combined array. the difference between the two is that when a key in an input array already exists in the result array, the function will adopt different processing methods. array_merge () overwrites the previous key/value (www.111cn.net) pair, replaces it with the key/value pair in the current input array, and array_merge_recursive () merges the two values, form a new array and use the original key as the array name. There is also a number combination form, that is, recursively append an array. The format is:

Array array_merge_recursive (array key, array values)

Example:

The Code is as follows:
<? Php
$ Array1 = array ("AA" => "A", "BB" => "B", "CC" => "C ", "DD" => "D ");
$ Array2 = array ("AA" => "1", "BB" => "2", "CC" => "3 ", "DD" => "4 ");
$ Array3 = array ("AA" => "! "," BB "=>" @ "," CC "=>" # "," DD "=>" $ ");
$ ArrayX = array_merge ($ array1, $ array2, $ array3); // merge Arrays
Print_r ($ arrayX );
?>


Connect two Arrays

The array_combine () function returns a new array consisting of a set of submitted keys and corresponding values. The format is:

Array array_merge (array array1, array array2 [..., Array arrayN])

Note: The two input arrays must be of the same size and cannot be empty. Example:

The Code is as follows:
<? Php
$ Array1 = array ("AA" => "A", "BB" => "B", "CC" => "C ", "DD" => "D ");
$ Array2 = array ("AA" => "1", "BB" => "2", "CC" => "3 ", "DD" => "4 ");
$ Array3 = array ("AA" => "! "," BB "=>" @ "," CC "=>" # "," DD "=>" $ ");
$ ArrayX = array_merge ($ array1, $ array2, $ array3); // merge Arrays
Print_r ($ arrayX );
?>

From: http://www.111cn.net/phper/php/43846.htm


How to Use array_merge () in php to merge n Arrays

For example, for result, OK?
Then we can help you write the process.

How to merge several two-dimensional arrays into a two-dimensional array in php?

$ A1 = array (1, 2, 4 );
$ A2 = array (a1, b2, c3, d4 );
Array_merge ($ a1, $ a2)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.