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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.