$arr = [2 => 'a', 3 => 'b', 4 => 'c'];
$arr1 = array_merge($arr);
$arr_TMP = ['A' => 'a', 'B' => 'b', 'C' => 'c'];
$arr2 = array_values($arr_TMP);
dump($arr1);
/**
* array_merge : 如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引,索引从0开始
* array(3) {
[0] => string(1) "a"
[1] => string(1) "b"
[2] => string(1) "c"
}
*/
dump($arr2);
/**
* array_values:返回数组中所有的值并给其建立数字索引,索引从0开始
* array(3) {
[0] => string(1) "a"
[1] => string(1) "b"
[2] => string(1) "c"
}
*/
$arr3 = ['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e'];
array_splice($arr3,1,2);
dump($arr3);
/**
* array_splice:数组中移除元素,从索引第一个开始移除两个元素,BC 两个元素被移除
* array(3) {
["A"] => string(1) "a"
["D"] => string(1) "d"
["E"] => string(1) "e"
}
*/
$arr4 = ['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e'];
$arr4_add = ['M' => 'm','N'=>'n','O'=>'o'];
array_splice($arr4,1,2,$arr4_add);
dump($arr4);
/**
* array_splice:移除后插入新的数组,插入的新数组会被重置索引
* array(6) {
["A"] => string(1) "a"
[0] => string(1) "m"
[1] => string(1) "n"
[2] => string(1) "o"
["D"] => string(1) "d"
["E"] => string(1) "e"
}
*/
$arr_null = [
'A'=>'',
'B'=>'b',
'C'=>'c',
'D'=>'',
];
$arr_null = array_filter($arr_null);
dump($arr_null);
/**
* array_filter:过滤数组中空值
* array(2) {
["B"] => string(1) "b"
["C"] => string(1) "c"
}
*/
php 常用数组处理 将数组重新排序 生成索引数组
最新推荐文章于 2021-06-23 10:39:22 发布