PHP program to merge the values ​​​​of two numeric key arrays_PHP tutorial

WBOY
Release: 2016-07-13 10:12:58
Original
950 people have browsed it

PHP program to merge the values ​​​​of two numeric key arrays

Array_merge is the most reliable function in php. When we use the array_merge function to merge below, we mainly use the array_merge function to merge the two numeric key arrays. The key values ​​of the array are processed as numbers.

You need to understand a basic knowledge point first

You can use + or array_merge to combine two arrays in PHP, but there are still differences between them, and if you don’t understand these differences clearly, it will be fatal in the project!

The main difference is that if the same key name appears in two or more arrays, the key name is divided into a string or a number. Please note

1) When the key name is a number, the value after array_merge() will not overwrite the original value, but will be appended to the end. However, the +merged array will return the first value as the final result, and the Those values ​​in the subsequent arrays with the same key names are "discarded" (not overwritten)

 2) When the key name is a string, array_merge() will overwrite the previous value with the same key name, but + will still return the first value as the final result, and the subsequent arrays with the same key name will be returned Those values ​​are "discarded" (not overwritten).

The code is as follows

$a = array('a', 'b');
$b = array('c', 'd');
$c = $a + $b;
var_dump($a);
var_dump(array_merge($a, $b));

//Output:

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = array(
0 => 'a',
1 => 'b'
);
$b = array(
0 => 'c',
1 => 'b'
);
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));

//输出:

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = array('a', 'b');
$b = array(
'0' => 'c',
1 => 'b'
);
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));

//输出:
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)

++++++++++++++++++++++++++++++++++++++++++

$a = array(
0 => 'a',
1 => 'b'
);
$b = array(
'0' => 'c',
'1' => 'b'
);
$c = $a + $b;
var_dump($c);
var_dump(array_merge($a, $b));

输出:

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)

array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'b' (length=1)

Okay, that’s enough. Let’s take a look at the value of merging two numeric key arrays

The code is as follows

/**
* PHP combines the values ​​​​of 2 numeric key arrays
*
* @param array $arr1
* @param array $arr2
* @return array
* @author www.111cn.net
*/
function new_array_merge($arr1, $arr2)
{
$arr = array_flip($arr1) + array_flip($arr2);
return array_keys($arr);
}

$arr1 = array('aa', 'bb', 'cc');
$arr2 = array('aa2', 'bb', 'cc2');

$arr = new_array_merge($arr1, $arr2);
print_r($arr);

Output:

Array
(
[0] => aa
[1] =>bb
[2] => cc
[3] => aa2
[4] => cc2
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/917546.htmlTechArticlePHP program to merge the values ​​​​of two numeric key arrays Array_merge is the most reliable function in php, below When we use the array_merge function to merge, we mainly focus on the keys of the two arrays...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!