Methods for merging, appending, and joining PHP arrays (Array_merge (), Array_combine () functions)

Source: Internet
Author: User
In this paper, we introduce the method of merging, appending and joining arrays in PHP, mainly the usage of function array_merge () and Array_combine (). Refer to a friend who needs it.

1, first, merge the array.

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) can combine one or more arrays of cells, and the values in an array are appended 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.

Example:

 
      

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

2, recursive append array array_merge_recursive () function is the same as Array_merge (), which can combine two or more numbers together to form a combined array. 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)

Example:

 
      "James" = "2"; $class = Array ("Micky" + +, "John"); $classScores = Array_merge_recursive ($class 1, $c LASS2);p Rint_r ($classScores);? >

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

3, connecting 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.

Example:

 
      

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

About the method and instance of merging, appending and connecting the array in PHP, we introduce this, and hope to help everyone.

The programmer's home, wish everybody to study the progress.

  • 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.