search
Homephp教程php手册PHP合并2个数字键数组的值的程序

PHP合并2个数字键数组的值的程序

   php中数组合并array_merge是最靠得住的函数了,下文我们在利用array_merge函数合并时主要是对于两个数组的键值为数字进行处理。

  先要了解一个基础知识点

  PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的!

  主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意

  1)键名为数字时,array_merge()后面的值将不会覆盖原来的值,而是附加到后面,但+合并数组则会把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(不是覆盖)

  2)键名为字符串时,array_merge()此时会覆盖掉前面相同键名的值,但+仍然把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(不是覆盖)。

代码如下  

$a = array('a', 'b');
$b = array('c', 'd');
$c = $a + $b;
var_dump($a);
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 '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)

  好了讲到这么多了我们来看看下合并2个数字键数组的值

代码如下  

/**
* PHP合并2个数字键数组的值
*
* @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);

输出:

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version