<!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>Returns a sub-vector of a given range</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-vector.shift.html">Ds\Vector::shift</a></div> <div class="next" style="text-align: right; float: right;"><a href="ds-vector.sort.html">Ds\Vector::sort</a></div> <div class="up"><a href="class.ds-vector.html">Vector</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="ds-vector.slice" class="refentry"> <div class="refnamediv"> <h1 class="refname">Ds\Vector::slice</h1> <p class="verinfo">(PECL ds >= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Vector::slice</span> — <span class="dc-title"> Returns a sub-vector of a given range </span></p> </div> <div class="refsect1 description" id="refsect1-ds-vector.slice-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><span class="type Ds\Vector">Ds\Vector</span></span> <span class="methodname"><strong>Ds\Vector::slice</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> ] )</div> <p class="para rdfs-comment"> Creates a sub-vector of a given range. </p> </div> <div class="refsect1 parameters" id="refsect1-ds-vector.slice-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">index</code></dt> <dd> <p class="para"> The index at which the sub-vector starts. </p> <p class="para"> If positive, the vector will start at that index in the vector. If negative, the vector will start that far from the end. </p> </dd> <dt> <code class="parameter">length</code></dt> <dd> <p class="para"> If a length is given and is positive, the resulting vector will have up to that many values in it. If the length results in an overflow, only values up to the end of the vector will be included. If a length is given and is negative, the vector will stop that many values from the end. If a length is not provided, the resulting vector will contain all values between the index and the end of the vector. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-ds-vector.slice-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A sub-vector of the given range. </p> </div> <div class="refsect1 examples" id="refsect1-ds-vector.slice-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-5200"> <p><strong>Example #1 <span class="function"><strong>Ds\Vector::slice()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$vector </span><span style="color: #007700">= new \</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Vector</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 /><br /></span><span style="color: #FF8000">// Slice from 2 onwards<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-></span><span style="color: #0000BB">slice</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Slice from 1, for a length of 3<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-></span><span style="color: #0000BB">slice</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Slice from 1 onwards<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-></span><span style="color: #0000BB">slice</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Slice from 2 from the end onwards<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-></span><span style="color: #0000BB">slice</span><span style="color: #007700">(-</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Slice from 1 to 1 from the end<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-></span><span style="color: #0000BB">slice</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</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 something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Ds\Vector Object ( [0] => c [1] => d [2] => e ) Ds\Vector Object ( [0] => b [1] => c [2] => d ) Ds\Vector Object ( [0] => b [1] => c [2] => d [3] => e ) Ds\Vector Object ( [0] => d [1] => e ) Ds\Vector Object ( [0] => b [1] => c [2] => d ) </pre></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-vector.shift.html">Ds\Vector::shift</a></div> <div class="next" style="text-align: right; float: right;"><a href="ds-vector.sort.html">Ds\Vector::sort</a></div> <div class="up"><a href="class.ds-vector.html">Vector</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>