mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 07:02:15 +01:00
217 lines
5.7 KiB
XML
Executable File
217 lines
5.7 KiB
XML
Executable File
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: cd943f94a013b74df8765ab8e1a620a916a64a85 Maintainer: HonestQiao Status: ready -->
|
||
<refentry xml:id="function.array-slice" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>array_slice</refname>
|
||
<refpurpose>从数组中取出一段</refpurpose>
|
||
</refnamediv>
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>array</type><methodname>array_slice</methodname>
|
||
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
||
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>length</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type>bool</type><parameter>preserve_keys</parameter><initializer>&false;</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
<function>array_slice</function> 返回根据 <parameter>offset</parameter> 和
|
||
<parameter>length</parameter> 参数所指定的 <parameter>array</parameter>
|
||
数组中的一段序列。
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>array</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
输入的数组。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>offset</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
如果 <parameter>offset</parameter> 非负,则序列将从 <parameter>array</parameter>
|
||
中的此偏移量开始。
|
||
</para>
|
||
<para>
|
||
如果 <parameter>offset</parameter> 为负,则序列将从
|
||
<parameter>array</parameter> 中距离末端这么远的地方开始。
|
||
</para>
|
||
<note>
|
||
<para>
|
||
参数 <parameter>offset</parameter> 标识的是数组中的位置,而不是键。
|
||
</para>
|
||
</note>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>length</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
如果给出了 <parameter>length</parameter> 并且为正,则序列中将具有这么多的单元。
|
||
</para>
|
||
<para>
|
||
如果 array 比 <parameter>length</parameter> 要短,只会保留有效的数组单元。
|
||
</para>
|
||
<para>如果给出了
|
||
<parameter>length</parameter> 并且为负,则序列将终止在距离数组末端这么远的地方。
|
||
</para>
|
||
<para>如果省略,则序列将从
|
||
<parameter>offset</parameter> 开始一直到 <parameter>array</parameter> 的末端。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>preserve_keys</parameter></term>
|
||
<listitem>
|
||
<note>
|
||
<para>
|
||
注意 <function>array_slice</function> 默认会重新排序并重置数组的数字索引。你可以通过将
|
||
<parameter>preserve_keys</parameter> 设为 &true; 来改变此行为。
|
||
无论本参数如何设置,都会保留字符串的键。
|
||
</para>
|
||
</note>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
返回其中一段。 如果 offset 参数大于 array 尺寸,就会返回空的 array。
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title><function>array_slice</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$input = array("a", "b", "c", "d", "e");
|
||
|
||
$output = array_slice($input, 2); // 返回 "c", "d" 和 "e"
|
||
$output = array_slice($input, -2, 1); // 返回 "d"
|
||
$output = array_slice($input, 0, 3); // 返回 "a", "b" 和 "c"
|
||
|
||
// 注意数组中 key 的不同
|
||
print_r(array_slice($input, 2, -1));
|
||
print_r(array_slice($input, 2, -1, true));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
Array
|
||
(
|
||
[0] => c
|
||
[1] => d
|
||
)
|
||
Array
|
||
(
|
||
[2] => c
|
||
[3] => d
|
||
)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
|
||
<para>
|
||
<example>
|
||
<title><function>array_slice</function> 于索引从 1 开始的 array</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$input = array(1 => "a", "b", "c", "d", "e");
|
||
print_r(array_slice($input, 1, 2));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
Array
|
||
(
|
||
[0] => b
|
||
[1] => c
|
||
)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title><function>array_slice</function> 与混合类型键的 array</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$ar = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');
|
||
print_r(array_slice($ar, 0, 3));
|
||
print_r(array_slice($ar, 0, 3, true));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
Array
|
||
(
|
||
[a] => apple
|
||
[b] => banana
|
||
[0] => pear
|
||
)
|
||
Array
|
||
(
|
||
[a] => apple
|
||
[b] => banana
|
||
[42] => pear
|
||
)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>array_splice</function></member>
|
||
<member><function>unset</function></member>
|
||
<member><function>array_chunk</function></member>
|
||
</simplelist>
|
||
</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:"~/.phpdoc/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
|
||
-->
|