1
0
mirror of https://github.com/php/doc-zh.git synced 2026-04-25 17:18:15 +02:00
Files
archived-doc-zh/reference/array/functions/array-splice.xml
T
Dallas Wang 39186f910d Updated.
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@194401 c90b9560-bf6c-de11-be94-00142212c4b1
2005-08-25 19:04:04 +00:00

163 lines
5.2 KiB
XML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="gb2312"?>
<!-- $Revision: 1.6 $ -->
<!-- $Author: dallas $ -->
<!-- EN-Revision: 1.15 Maintainer: dallas Status: ready -->
<refentry id="function.array-splice">
<refnamediv>
<refname>array_splice</refname>
<refpurpose>
把数组中的一部分去掉并用其它值取代
</refpurpose>
</refnamediv>
<refsect1>
<title>说明</title>
<methodsynopsis>
<type>array</type><methodname>array_splice</methodname>
<methodparam><type>array</type><parameter role="reference">input</parameter></methodparam>
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>
replacement
</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_splice</function><parameter>input</parameter> 数组中由
<parameter>offset</parameter><parameter>length</parameter>
指定的单元去掉,如果提供了 <parameter>replacement</parameter> 参数,则用
<parameter>replacement</parameter> 数组中的单元取代。返回一个包含有被移除单元的数组。
</para>
<para>
如果 <parameter>offset</parameter> 为正,则从 <parameter>input</parameter>
数组中该值指定的偏移量开始移除。如果 <parameter>offset</parameter>
为负,则从 <parameter>input</parameter> 末尾倒数该值指定的偏移量开始移除。
</para>
<para>
如果省略 <parameter>length</parameter>,则移除数组中从 <parameter>offset</parameter>
到结尾的所有部分。如果指定了 <parameter>length</parameter>
并且为正值,则移除这么多单元。如果指定了 <parameter>length</parameter>
并且为负值,则移除从 <parameter>offset</parameter> 到数组末尾倒数
<parameter>length</parameter> 为止中间所有的单元。小窍门:当给出了
<parameter>replacement</parameter> 时要移除从 <parameter>offset</parameter>
到数组末尾所有单元时,用 <literal>count($input)</literal> 作为 <parameter>length</parameter>
</para>
<para>
如果给出了 <parameter>replacement</parameter> 数组,则被移除的单元被此数组中的单元替代。如果
<parameter>offset</parameter><parameter>length</parameter>
的组合结果是不会移除任何值,则 <parameter>replacement</parameter>
数组中的单元将被插入到 <parameter>offset</parameter>
指定的位置。注意替换数组中的键名不保留。如果用来替换的值只是一个单元,那么不需要给它加上
<literal>array()</literal>,除非该单元本身就是一个数组。
</para>
<para>
以下表达式以同样方式修改了 <varname>$input</varname>
<table>
<title><function>array_splice</function> 等价表达式</title>
<tgroup cols="2">
<tbody>
<row>
<entry>
array_push($input, $x, $y)
</entry>
<entry>
array_splice($input, count($input), 0, array($x, $y))
</entry>
</row>
<row>
<entry>
array_pop($input)
</entry>
<entry>
array_splice($input, -1)
</entry>
</row>
<row>
<entry>
array_shift($input)
</entry>
<entry>
array_splice($input, 0, 1)
</entry>
</row>
<row>
<entry>
array_unshift($input, $x, $y)
</entry>
<entry>
array_splice($input, 0, 0, array($x, $y))
</entry>
</row>
<row>
<entry>
$input[$x] = $y // 对于键名和偏移量等值的数组
</entry>
<entry>
array_splice($input, $x, 1, $y)
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
返回一个包含被移除单元的数组。
</para>
<para>
<example>
<title><function>array_splice</function> 例子</title>
<programlisting role="php">
<![CDATA[
<?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");
?>
]]>
</programlisting>
</example>
</para>
<para>
参见 <function>array_slice</function><function>unset</function>
<function>array_merge</function>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->