Merge arrays in php

Source: Internet
Author: User
Combine multiple numbers. 1. Combine the array. the array_merge () function merges the array and returns a Union array. the obtained array starts with the first input array parameter and then follows the array.

Combine multiple numbers

1. Merge arrays

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

  1. Array array_merge (array array1 array2 ..., Arrayn)

Combine the elements of one or more arrays. the values of an array are appended to the previous array and returned as an array of results.

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

If only one array is assigned and the array is digitally indexed, the key name is re-indexed in a continuous manner. The example is as follows:

  1. $ Face = array ("j", "q", "k", "");
  2. $ Numbered = array ("2", "3", "4", "5", "6", "7", "8", "9 ");
  3. $ Cards = array_merge ($ face, $ numbered );
  4. Shuffle ($ cards );
  5. Print_r ($ cards );

This will return the following results:

  1. 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)

2. recursively append an 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, this function takes different processing methods. array_merge () overwrites the previous key/value pair, replace it with the key/value pair in the current input array, while array_merge_recursive () merges the two values to form a new array and uses the original key as the array name. Another form of array merging is recursively appending an array, in the form:

  1. Array array_merge_recursive (array key, array values)

Example:

  1. $ Class1 = array ("john" => 100, "james" => 85 );
  2. $ Class2 = array ("micky" => 78, "john" => 45 );
  3. $ Classscores = array_merge_recursive ($ class1, $ class2 );
  4. Print_r ($ classscores );

The following result is returned:

  1. Array ([john] => array ([0] => 100 [1] => 45) [james] => 85 [micky] => 78)

3. connect two arrays

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

  1. Array array_merge (array array1, array array2 [..., Array arrayn])

Note: the two input arrays must be of the same size and cannot be blank. the instance is as follows:

  1. $ Abbreviations = array ("al", "ak", "az", "ar ");
  2. $ States = array ("alabama", "alaska", "arizona", "arkansas ");
  3. $ Statemap = array_combine ($ abbreviations, $ states );
  4. Print_r ($ statemap );

This will return:

  1. Array ([al] => alabama [ak] => alaska [az] => arizona [ar] => arkansas)

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.