In PHP, the array_merge program combines two arrays _ PHP Tutorial

Source: Internet
Author: User
In PHP, array_merge merges two arrays. In php, we use the array_merge () function to merge data. the array_merge () function combines two or more numbers into an array. If the key name already exists, the key value of the key is the last merged data. in php, we use the array_merge () function () the function combines two or more numbers into an array. If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.
The code is as follows:


Echo "rn First case rn ";
$ A = array (1, 2, 3, 4, 5, 6 );
$ B = array (7,8, 9 );

$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );


Echo "rn second case rn ";
$ A = array ('A', 'B', 'C', 'D', 'e', 'F ');
$ B = array ('A', 'X', 'y ');

$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );


Echo "rn case 3 rn ";

$ A = array (
1 => 'A ',
2 => 'B ',
3 => 'C ',
4 => 'D ',
5 => 'e ',
6 => 'F ');
$ B = array (
1 => 'A ',
7 => 'X ',
8 => 'y ');

$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
?>

The result is as follows:

First case
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 4
[4] => 5
[5] => 6
)

Case 2
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
)
Array
(
[0] =>
[1] => x
[2] => y
[3] => d
[4] => e
[5] => f
)

Case 3
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[1] =>
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
[7] => x
[8] => y
)
Array
(
[1] =>
[7] => x
[8] => y
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
)

1) key name

When it is a number, array_merge () does not overwrite the original value, but + merges the array and returns the first value as the final result, the values of the following arrays with the same key names are discarded (not overwritten)

2) when the key name is a character, + still returns the first value as the final result, and discards the values with the same key name in the subsequent array, but array_merge () at this time, the values with the same key names will be overwritten.


Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed

Implement the aggregate () function. the array_merge () function combines two or more numbers into an array. If the key name already exists, the key value of the key is the last one...

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.