<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Extract a slice of the array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-shift.html">array_shift</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-splice.html">array_splice</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.array-slice" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_slice</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_slice</span> — <span class="dc-title">Extract a slice of the array</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-slice-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array_slice</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_keys</code><span class="initializer"> = false</span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>array_slice()</strong></span> returns the sequence of elements from the array <em><code class="parameter">array</code></em> as specified by the <em><code class="parameter">offset</code></em> and <em><code class="parameter">length</code></em> parameters. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-slice-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">array</code></em></span> <dd> <p class="para"> The input array. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">offset</code></em></span> <dd> <p class="para"> If <em><code class="parameter">offset</code></em> is non-negative, the sequence will start at that offset in the <em><code class="parameter">array</code></em>. If <em><code class="parameter">offset</code></em> is negative, the sequence will start that far from the end of the <em><code class="parameter">array</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> If <em><code class="parameter">length</code></em> is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the <em><code class="parameter">length</code></em>, then only the available array elements will be present. If <em><code class="parameter">length</code></em> is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from <em><code class="parameter">offset</code></em> up until the end of the <em><code class="parameter">array</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">preserve_keys</code></em></span> <dd> <p class="para"> Note that <span class="function"><strong>array_slice()</strong></span> will reorder and reset the numeric array indices by default. You can change this behaviour by setting <em><code class="parameter">preserve_keys</code></em> to <strong><code>TRUE</code></strong>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-slice-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the slice. </p> </div> <div class="refsect1 changelog" id="refsect1-function.array-slice-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.2.4</td> <td> The default value of the <em><code class="parameter">length</code></em> parameter was changed to <em>NULL</em>. A <em>NULL</em> <em><code class="parameter">length</code></em> now tells the function to use the length of <em><code class="parameter">array</code></em>. Prior to this version, a <em>NULL</em> <em><code class="parameter">length</code></em> was taken to mean a zero length (nothing will be returned). </td> </tr> <tr> <td>5.0.2</td> <td> The optional <em><code class="parameter">preserve_keys</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.array-slice-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4982"> <p><strong>Example #1 <span class="function"><strong>array_slice()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b"</span><span style="color: #007700">, </span><span style="color: #DD0000">"c"</span><span style="color: #007700">, </span><span style="color: #DD0000">"d"</span><span style="color: #007700">, </span><span style="color: #DD0000">"e"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "c", "d", and "e"<br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "d"<br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "a", "b", and "c"<br /><br />// note the differences in the array keys<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d ) </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-slice-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a> - Remove a portion of the array and replace it with something else</span></li> <li class="member"> <span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - Unset a given variable</span></li> <li class="member"> <span class="function"><a href="function.array-chunk.html" class="function" rel="rdfs-seeAlso">array_chunk()</a> - Split an array into chunks</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-shift.html">array_shift</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-splice.html">array_splice</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>