PHP merge, append and concatenate arrays _php tutorial

Source: Internet
Author: User

Merging arrays

The Array_merge () function merges the arrays together to return an array of unions. The resulting array starts with the first input array parameter, forcing it sequentially in the order in which the array parameters appear later. The form is:

Array Array_merge (array array1 array2...,arrayn)

Merges the cells of one or more arrays and appends the values in an array to the previous array. Returns an array as the result.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.

Examples are as follows:

$face = Array ("J", "Q", "K", "A"), $numbered = Array ("2", "3", "4", "5", "6", "7", "8", "9"), $cards = Array_merge ($face, $ Numbered) Shuffle ($cards);p rint_r ($cards);

This will return the results shown below, running the code:

Array ([0] = A [1] = 4 [2] = 9 [3] = 3 [4] + K [5] = 7 [6] = 5 [7] = Q [8] = 6 [9] =& Gt 8 [Ten] = 2 [one] and J)

Recursive append array

The array_merge_recursive () function is the same as Array_merge () and can combine two or more numbers together to form an array of unions. The difference between the two is that when a key in an input array is already in the result array, the function takes a different approach. Array_merge () overrides the previously existing key/value pair, replacing it with the key/value pair in the current input array, and array_merge_recursive () merges two values together to form a new array with the original key as the array name. There is also the form of an array merge, which is a recursive append array. The form is:

Array array_merge_recursive (array key,array values)

Examples are as follows:

$class 1 = Array ("john" = +, "James" =), $class 2 = Array ("Micky" + +, "John"); $classScores = Arra Y_merge_recursive ($class 1, $class 2);p Rint_r ($classScores);

This will return the following results:

Array ([John] = = Array ([0] = [1] = () [James] = [Micky] = 78)

Connect two arrays

The Array_combine () function gets a new array that consists of a set of committed keys and corresponding values. The form is:

Array Array_merge (array Array1,array array2[...,array Arrayn])

Note that two input arrays must be of the same size and cannot be empty. Examples are as follows:

$abbreviations = Array ("AL", "AK", "AZ", "AR"), $states = Array ("Alabama", "Alaska", "Arizona", "Arkansas"); $stateMap = Array_combine ($abbreviations, $states);p Rint_r ($STATEMAP);

This will return:

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

http://www.bkjia.com/PHPjc/752542.html www.bkjia.com true http://www.bkjia.com/PHPjc/752542.html techarticle The merge array array_merge () function merges the arrays together and returns an array of the unions. The resulting array starts with the first input array parameter, in the order in which the array parameters appear later ...

  • Related Article

    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.