search
Homephp教程php手册PHP中数组合并的两种方法及区别介绍

PHP中数组合并的两种方法及区别介绍

Jun 13, 2016 am 11:57 AM
phpintroduceassociationthe differencemergearraymethodof

PHP数组合并两种方法及区别
如果是关联数组,如下:

复制代码 代码如下:


$a = array(
'where' => 'uid=1',
'order' => 'uid',
);
$b = array(
'where' => 'uid=2',
'order' => 'uid desc',
);



1. array_merge,如果两个数组存在相同的key,后面的一个会覆盖前面的

复制代码 代码如下:


$c = array_merge($a, $b);
var_export($c);//结果与原来的$b相同
$d = array_merge($b, $a);
var_export($d);//结果与原来的$a相同


2. "+"操作符,如果两个数组存在相同的key,前面的一个会覆盖后面的

复制代码 代码如下:


$c = $a + $b;
var_export($c);//结果与原来的$a相同
$d = $b + $a;
var_export($d);//结果与原来的$b相同



如果是数字索引数组,如下:

复制代码 代码如下:


$a = array(
1 => '1111111',
2 => '222222222'
);
$b = array(
4 => '33333333333',
1 => '444444444'
);


1. array_merge. 效果类似代码foreach每个数组元素,然后将每个元素压入一个新堆栈当中

复制代码 代码如下:


$c = array_merge($a, $b);
var_export($c);
$d = array_merge($b, $a);
var_export($d);


输出:
array (
0 => '1111111',
1 => '222222222',
2 => '33333333333',
3 => '444444444',
)
array (
0 => '33333333333',
1 => '444444444',
2 => '1111111',
3 => '222222222',
)
2. "+"操作符. 效果类似代码foreach每个数组元素,然后将每个元素压入一个新堆栈当中,如果同样的key已经存在则不处理

复制代码 代码如下:


$c = $a + $b;
var_export($c);
$d = $b + $a;
var_export($d);


输出:
array (
1 => '1111111',
2 => '222222222',
4 => '33333333333',
)
array (
4 => '33333333333',
1 => '444444444',
2 => '222222222',
)
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 Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!