Array_merge program to merge two arrays in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:14:58
Original
859 people have browsed it

In php, we use the array_merge() function to merge data. The array_merge() function merges two or more arrays into one array. If there are duplicate key names, the key value of the key will be the value corresponding to the last key name (the later one will overwrite the previous one). If the array is numerically indexed, the key names are re-indexed consecutively.

The code is as follows Copy code


echo "rnFirst 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+$a;
print_r($c);


echo "rnSecond 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+$a;
print_r($c);


echo "rnThe third case 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:

The first situation
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 situation
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 situation
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

When

is a number, array_merge() will not overwrite the original value, but +merging the array will return the first value as the final result, and "discard" those values ​​in the subsequent arrays with the same key name. Remove (not cover)

2) When the key name is a character, + still returns the first value as the final result, and "discards" those values ​​in the subsequent arrays with the same key name, but array_merge() will overwrite them at this time The value of the same key name before


Note: If only an array is input to the array_merge() function, and the keys are integers, the function will return a new array with integer keys, whose keys are re-indexed starting with 0

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628937.htmlTechArticleIn php, we use the array_merge() function to realize the merging of data. The array_merge() function combines two or more Arrays are combined into one array. If there are duplicate key names, the key value of the key is the last one...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template