PHP combines multiple numbers into an array and the instance code

Source: Internet
Author: User

1. Merge Arrays
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: CopyCode The Code is as follows: 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:Copy codeThe Code is as follows: $ 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 and run the Code:Copy codeThe Code is as follows: 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, 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. Another form of array merging is recursively appending an array. The format is:
View sourceprint? Array array_merge_recursive (array key, array values)

Example:Copy codeThe Code is as follows: $ 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:
View sourceprint? 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 set of submitted keys and corresponding values. The format is:
View sourceprint? 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:Copy codeThe Code is as follows: $ abbreviations = array ("Al", "AK", "az", "Ar ");
$ States = array ("Alabama", "Alaska", "Arizona", "Arkansas ");
$ Statemap = array_combine ($ abbreviations, $ States );
Print_r ($ statemap );

This will return:Copy codeThe Code is as follows: 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.