Detailed description of the Combined Array of array_merge in php

Source: Internet
Author: User
Tags arrays

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: Copy code


<? 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: Copy code

$ 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: Copy code
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 existing key/value pairs and replaces them with the key/value pairs in the current input array. 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: Copy code

<? 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: Copy code

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

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.