search
HomeheadlinesPHP commonly used array functions (3)

* 1. Split and merge

* 1. Split:array_chunk($arr,$num,[true]):$numThe number of elements in each group, true keeps the original index

* 2. Merge: array_merge($arr1,$arr2,...); associated key names with the same name will be automatically overwritten, and the index will be rearranged and automatically accumulated

* 2. Take out some elements

* 1.array_slice($arr, $offset, $length, true): Take out the specified number of elements from the specified position, true does not reset the index

* 3. Delete or replace some elements

* array_splice(&input,$offset [,$length=count($input) [,$replacement = array()]])

* Delete elements from the specified position, or replace them with new array elements

//1. Split: array_chunk($arr,$num,[true]): $num number of elements in each group, true keeps the original index

$arr = [3,5,10,4,'a'=>'中国','php',39,'mysql','java',true,[100,200,300]];

//Each group of 3 Split and keep the original index

// print_r(array_chunk($arr,3,true));

// 2. Merge: array_merge($arr1,$arr2,... );

$arr1 = ['name'=>'peter','course'=>'php','grade'=>60, 5=>'jQuery'];
$arr2 = ['name'=>'朱老师','sex'=>'男','grade'=>90,'job'=>'lecturer', 5=>'php'];

//The associated key names with the same name will be automatically overwritten, and the index key names will be rearranged and automatically accumulated, which is very suitable for user-defined configuration files to update system configuration

$arr1=array_merge($arr1, $arr2);//合并后再更新第一个数组
print_r($arr1); //查看合并后的数组,常用于配置文件更新操作

//3.array_slice ($arr, $offset, $length, true): Remove the specified number of elements from the specified position, true does not reset the numeric index

$arr2 = ['name'=>'朱老师','sex'=>'男','grade'=>90,'job'=>'lecturer', 5=>'php'];
print_r(array_slice($arr2,2,4)); //返回取出的元素组成的数组
print_r(array_slice($arr2,2,4,true)); //true: 不重置数字索引

//4.array_splice(&input,$offset): Delete or replace Partial elements

$city = ['北京', '上海', '广州', '深圳', '重庆', '天津'];

//Only keep the first 2 elements, and delete them starting from the index position 2: Guangzhou

print_r(array_splice($city, 2)); //返回删除
print_r($city);  //查看原数据,发现只有前二个元素啦
$city = ['北京', '上海', '广州', '深圳', '重庆', '天津'];

//The length is a negative number, which means from the negative number to the starting point elements, in this example -1 is Tianjin, 2,-1 refers to the data between Guangzhou and Chongqing

print_r(array_splice($city, 2,-1)); 
print_r($city);  //查看原数据,只前二个北京上海和最后一个天津
$city = ['北京', '上海', '广州', '深圳', '重庆', '天津'];
print_r(array_splice($city, -2, 1, ['合肥', '南京'])); //返回删除的重庆
print_r($city);  //查看原数据,重庆的位置由合肥,南京代替
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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor