<!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, PHP 7)</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"> = <strong><code>FALSE</code></strong></span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>array_slice()</strong></span> returns the sequence of elements from the array <code class="parameter">array</code> as specified by the <code class="parameter">offset</code> and <code class="parameter">length</code> parameters. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-slice-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">array</code></dt> <dd> <p class="para"> The input array. </p> </dd> <dt> <code class="parameter">offset</code></dt> <dd> <p class="para"> If <code class="parameter">offset</code> is non-negative, the sequence will start at that offset in the <code class="parameter">array</code>. If <code class="parameter">offset</code> is negative, the sequence will start that far from the end of the <code class="parameter">array</code>. Note that the <code class="parameter">offset</code> denotes the position in the array, not the key. </p> </dd> <dt> <code class="parameter">length</code></dt> <dd> <p class="para"> If <code class="parameter">length</code> is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the <code class="parameter">length</code>, then only the available array elements will be present. If <code class="parameter">length</code> 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 <code class="parameter">offset</code> up until the end of the <code class="parameter">array</code>. </p> </dd> <dt> <code class="parameter">preserve_keys</code></dt> <dd> <p class="para"> Note that <span class="function"><strong>array_slice()</strong></span> will reorder and reset the integer array indices by default. You can change this behaviour by setting <code class="parameter">preserve_keys</code> to <strong><code>TRUE</code></strong>. String keys are always preserved, regardless of this parameter. </p> </dd> </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. If the offset is larger than the size of the array then returns an empty array. </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 <code class="parameter">length</code> parameter was changed to <em>NULL</em>. A <em>NULL</em> <code class="parameter">length</code> now tells the function to use the length of <code class="parameter">array</code>. Prior to this version, a <em>NULL</em> <code class="parameter">length</code> was taken to mean a zero length (nothing will be returned). </td> </tr> <tr> <td>5.0.2</td> <td> The optional <code class="parameter">preserve_keys</code> 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-6196"> <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> <p class="para"> <div class="example" id="example-6197"> <p><strong>Example #2 <span class="function"><strong>array_slice()</strong></span> and one-based array</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: #0000BB">1 </span><span style="color: #007700">=> </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 /></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">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</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] => b [1] => c ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-6198"> <p><strong>Example #3 <span class="function"><strong>array_slice()</strong></span> and array with mixed keys</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$ar </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">=></span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">=></span><span style="color: #DD0000">'banana'</span><span style="color: #007700">, </span><span style="color: #DD0000">'42'</span><span style="color: #007700">=></span><span style="color: #DD0000">'pear'</span><span style="color: #007700">, </span><span style="color: #DD0000">'d'</span><span style="color: #007700">=></span><span style="color: #DD0000">'orange'</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">$ar</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">));<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">$ar</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: #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 ( [a] => apple [b] => banana [0] => pear ) Array ( [a] => apple [b] => banana [42] => pear ) </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>