PHP merge, append and connection array _ PHP Tutorial

Source: Internet
Author: User
PHP merges, appends, and connects to arrays. The array_merge () function combines the arrays and returns a Union array. The obtained array starts with the first input array parameter and merges the array according to the order in which the following array parameters appear.

The array_merge () function combines arrays and returns a Union array. The obtained array starts with the first input array parameter and is immediately added according to the order in which the following array parameters appear. The format is:

array array_merge (array array1 array2…,arrayN)

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:

$face = array("J","Q","K","A");$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 and run the 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:

$class1 = array("John" => 100, "James" => 85);$class2 = array("Micky" => 78, "John" => 45);$classScores = array_merge_recursive($class1, $class2);print_r($classScores);

The following result is returned:

Array ( [John] => Array ( [0] => 100 [1] => 45 ) [James] => 85 [Micky] => 78 )
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:

$abbreviations = array("AL","AK","AZ","AR");$states = array("Alabama","Alaska","Arizona","Arkansas");$stateMap = array_combine($abbreviations,$states);print_r($stateMap);

This will return:

Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona [AR] => Arkansas )

The merge array_merge () function combines the arrays and returns a Union array. The obtained array starts with the first input array parameter, in the order in which the following array parameters appear...

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.