PHP array_merge合并拆分两个数组
对数组的array_merge函数和+运算符比较迷惑,写了个小程序比较下发现了他们的不同。 特别是+运算符,他的意思是,将右边的数组单元(去重复)追加到左边数组的后面。
代码如下 | 复制代码 |
$arr1=array("a","b","c"); $myarray=array_merge($arr1,$arr2); $myarray=array_unique($myarray); print_r($myarray); |
例
$array1=array(1, 2);//数组1
代码如下
复制代码
$array2=array(2, 3);//数组2
$array3=array_merge($array1, $array2);//合并数组;
$array3=array_unique($array3);//移除数组中重复的值
?>
例子
代码如下 | 复制代码 |
echo "rn第一种情况rn"; 结果如下: 第一种情况 |
拆分数组 array_slice()
array_slice()函数将返回数组中的一部分,从键offset开始,到offset+length位置结束。其形式:
Php代码
1.array array_slice (array array, int offset[,int length])
array array_slice (array array, int offset[,int length])
offset 为正值时,拆分将从距数组开头的offset 位置开始;如果offset 为负值,则拆分从距数组末尾的offset 位置开始。如果省略了可选参数length,则拆分将从offset 开始,一直到数组的最后一个元素。如果给出了length 且为正数,则会在距数组开头的offset+length 位置结束。相反,如果给出了length且为负数,则在距数组开头的count(input_array)-|length|位置结束。考虑一个例子:
Php代码
代码如下 | 复制代码 |
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); // output |
然后我们使用下负长度:
Php代码
代码如下 | 复制代码 |
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon"); // output |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

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.
