php-doc-en/reference/array/functions/array-slice.xml
2019-04-01 20:22:42 +00:00

256 lines
6.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.array-slice" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_slice</refname>
<refpurpose>Extract a slice of the array</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>int</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> returns the sequence of elements
from the array <parameter>array</parameter> as specified by the
<parameter>offset</parameter> and <parameter>length</parameter>
parameters.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
The input array.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>offset</parameter></term>
<listitem>
<para>
If <parameter>offset</parameter> is non-negative, the sequence will
start at that offset in the <parameter>array</parameter>.
</para>
<para>
If <parameter>offset</parameter> is negative, the sequence will
start that far from the end of the <parameter>array</parameter>.
</para>
<note>
<para>
The <parameter>offset</parameter> parameter denotes the position
in the array, not the key.
</para>
</note>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>length</parameter></term>
<listitem>
<para>
If <parameter>length</parameter> is given and is positive,
then the sequence will have up to that many elements in it.
</para>
<para>
If the array is shorter than the <parameter>length</parameter>,
then only the available array elements will be present.
</para>
<para>
If <parameter>length</parameter> is given and is negative then the
sequence will stop that many elements from the end of the array.
</para>
<para>
If it is omitted, then the sequence will have everything
from <parameter>offset</parameter> up until the end of the
<parameter>array</parameter>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>preserve_keys</parameter></term>
<listitem>
<note>
<para>
<function>array_slice</function> will reorder and reset the
integer array indices by default. This behaviour can be changed
by setting <parameter>preserve_keys</parameter> to &true;.
String keys are always preserved, regardless of this parameter.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the slice. If the offset is larger than the size of the array,
an empty array is returned.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.2.4</entry>
<entry>
The default value of the <parameter>length</parameter> parameter was
changed to &null;. A &null; <parameter>length</parameter> now tells
the function to use the length of <parameter>array</parameter>.
Prior to this version, a &null; <parameter>length</parameter> was
taken to mean a zero length (nothing will be returned).
</entry>
</row>
<row>
<entry>5.0.2</entry>
<entry>
The optional <parameter>preserve_keys</parameter> parameter was added.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>array_slice</function> examples</title>
<programlisting role="php">
<![CDATA[
<?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"
// note the differences in the array keys
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> and one-based 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> and array with mixed keys</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_chunk</function></member>
<member><function>array_splice</function></member>
<member><function>unset</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
-->