Blogger Information
Blog 87
fans 0
comment 0
visits 59252
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第五章:4.分割合并删除与替换
黄忠倚的博客
Original
608 people have browsed it

实例

<?php
echo '<h2>常用的数组函数(三)</h2><hr color="green">';
/医院
 * 1.分割与合并:array_chunk(),array_merge()
 * 2.取出部分元素:array_slice()
 * 3.删除或替换部分元素:array_splice()
 */

echo '<pre>';
//1.分割array_chunk($arr, $num, {true})
$arr = [3,5,10,4,'a'=>'中国',39,'mysql','java',true,[100,200,300]];
//每三个元素为一组进行分割
echo count($arr);
print_r(array_chunk($arr,3));
print_r(array_chunk($arr,3,true));

//2.array_merge($arr1,$arr2,......)
$arr1 = ['name'=>'Kevin','course'=>'php','grade'=>60,5=>'jQuery'];
$arr2 = ['name'=>'黄忠倚','sex'=>'男','grade'=>90,'job'=>'Students',5=>'php'];
$arr1 = array_merge($arr1,$arr2);
print_r($arr1);  //同名键名的处理方式:关联部分覆盖,索引部分重排

//3.array_slice($arr,$offset,$length,true): 从指定位置取出指定数量的元素,true不重置索引
// $arr2 = ['name'=>'黄忠倚','sex'=>'男','grade'=>90,'job'=>'students',5=>'php'];
// print_r(array_slice($arr2,2,4));
// print_r(array_slice($arr2,2,4,true));
// print_r($arr2);
// 4.array_splice(&$arr,$offset,$length,$arr1):删除或替换部分元素
$city = ['北京','上海','广州','深圳','重庆','天津'];
print_r(array_splice($city,2,-2)); //返回的是删除掉的元素
print_r($city);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post