array_slice和array_splice的使用

参考:https://www.jb51.net/article/95001.htm

其实个人总结:array_slice就是取出数组的部分;array_splice还可以替换甚至插入

//array_slice的使用
<?php
$input = array("a", "b", "c", "d", "e");
 
$output = array_slice($input, 2);   // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3);  // returns "a", "b", and "c"
 
print_r(array_slice($input, 2, -1)); // array(0 => 'c', 1 => 'd');
print_r(array_slice($input, 2, -1, true)); // array(2 => 'c', 1 => 'd');
//array_splice的使用:
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input变为 array("red", "green")
 
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input变为 array("red", "yellow")
 
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input变为 array("red", "orange")
 
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input为 array("red", "green",
//     "blue", "black", "maroon")
 
$input = array("red", "green", "blue", "yellow");        //还可以插入!!
array_splice($input, 3, 0, "purple");
// $input为 array("red", "green",
//     "blue", "purple", "yellow");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值