PHP Array_merge merging two arrays program _php tutorial

Source: Internet
Author: User
Merging data in PHP we use the Array_merge () function to implement the Array_merge () function to combine two or more arrays into an array. If the key name is duplicated, the key's key value is the value corresponding to the last key name (followed by overwriting the previous one). If the array is a numeric index, the key name is re-indexed in a sequential manner.
The code is as follows Copy Code


echo "RN The first case of 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 + $a;
Print_r ($c);


echo "RN The second case of 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 + $a;
Print_r ($c);


echo "RN The third case of 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 + $a;
Print_r ($c);
?>

The results are 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
)

The second case
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
[6] = a
[7] = X
[8] = y
)
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
)
Array
(
[0] = a
[1] = X
[2] = y
[3] = d
[4] = = E
[5] = f
)

The third case
Array
(
[0] = a
[1] = b
[2] = C
[3] = d
[4] = = E
[5] = f
[6] = a
[7] = X
[8] = y
)
Array
(
[1] = a
[2] = b
[3] = C
[4] = d
[5] = = E
[6] = f
[7] = X
[8] = y
)
Array
(
[1] = a
[7] = X
[8] = y
[2] = b
[3] = C
[4] = d
[5] = = E
[6] = f
)

1) Key Name

As a number, array_merge () does not overwrite the original value, but the + merge array will return the first occurrence as the final result, and the values of the following array with the same key name "Discard" (not overwrite)

2) When the key name is a character, + still returns the first occurrence as the final result, and discards the value of the following array with the same key name, but Array_merge () overwrites the previous value of the same key name


Note: If you only enter an array into the Array_merge () function and the key name is an integer, the function returns a new array with an integer key name, with the key name re-indexed starting at 0

http://www.bkjia.com/PHPjc/628937.html www.bkjia.com true http://www.bkjia.com/PHPjc/628937.html techarticle merging data in PHP we use the Array_merge () function to implement the Array_merge () function to combine two or more arrays into an array. If the key name is duplicated, the key value is the last ...

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