Home  >  Article  >  PHP commonly used array functions (3)

PHP commonly used array functions (3)

无忌哥哥
无忌哥哥Original
2018-06-28 10:28:161837browse

* 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