谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

array_splice去掉数组中的某一部分并用其它值取代

来源:网络转载 浏览:31850次 时间:2023-09-15

array_splice

(PHP 4, PHP 5, PHP 7)

array_splice — 去掉数组中的某一部分并用其它值取代

说明

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

把 input 数组中由 offset 和 length 指定的单元去掉,如果提供了 replacement 参数,则用其中的单元取代。

注意 input 中的数字键名不被保留。

Note: 如果 replacement 不是数组,会被 类型转换 成数组 (例如: (array) $replacement)。 当传入的 replacement 是个对象或者 NULL,会导致未知的行为出现。

参数

  • input

  • 输入的数组。

  • offset

  • 如果 offset 为正,则从 input 数组中该值指定的偏移量开始移除。如果 offset 为负,则从 input 末尾倒数该值指定的偏移量开始移除。

  • length

  • 如果省略 length,则移除数组中从 offset 到结尾的所有部分。如果指定了 length 并且为正值,则移除这么多单元。如果指定了 length 并且为负值,则移除从 offset 到数组末尾倒数 length 为止中间所有的单元。 如果设置了 length 为零,不会移除单元。 小窍门:当给出了 replacement 时要移除从 offset 到数组末尾所有单元时,用 count($input) 作为 length

  • replacement

  • 如果给出了 replacement 数组,则被移除的单元被此数组中的单元替代。

    如果 offset 和 length 的组合结果是不会移除任何值,则 replacement 数组中的单元将被插入到 offset指定的位置。 注意替换数组中的键名不保留。

    如果用来替换 replacement 只有一个单元,那么不需要给它加上 array(),除非该单元本身就是一个数组、一个对象或者 NULL

返回值

返回一个包含有被移除单元的数组。

范例

Example #1 array_splice() 例子

<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
//          "blue", "purple", "yellow");
?>

Example #2 array_splice() 例子

以下表达式以同样方式修改了 $input

<?php

// 添加两个新元素到 $input
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));

// 移除 $input 中的最后一个元素
array_pop($input);
array_splice($input, -1);

// 移除  $input 中第一个元素
array_shift($input);
array_splice($input, 0, 1);

// 在 $input 的开头插入一个元素
array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));

// 在 $input  的索引  $x 处替换值
$input[$x] = $y; // 对于键名和偏移量等值的数组
array_splice($input, $x, 1, $y);
?>

参见

  • array_slice() - 从数组中取出一段

  • unset() - 释放给定的变量

  • array_merge() - 合并一个或多个数组


http://php.net/manual/zh/function.array-splice.php


  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net