📄 function.array-slice.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Extract a slice of the array</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>array_slice</b></b></span> ( <span class="methodparam"><span class="type">array</span> <tt class="parameter">$array</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$offset</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$length</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$preserve_keys</tt></span> ]] )</div> <p class="para rdfs-comment"> <b>array_slice()</b> returns the sequence of elements from the array <i><tt class="parameter">array</tt></i> as specified by the <i><tt class="parameter">offset</tt></i> and <i><tt class="parameter">length</tt></i> parameters. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">array</tt></i></span> <dd> <p class="para"> The input array. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">offset</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">offset</tt></i> is non-negative, the sequence will start at that offset in the <i><tt class="parameter">array</tt></i>. If <i><tt class="parameter">offset</tt></i> is negative, the sequence will start that far from the end of the <i><tt class="parameter">array</tt></i>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">length</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">length</tt></i> is given and is positive, then the sequence will have that many elements in it. If <i><tt class="parameter">length</tt></i> 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 <i><tt class="parameter">offset</tt></i> up until the end of the <i><tt class="parameter">array</tt></i>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">preserve_keys</tt></i></span> <dd> <p class="para"> Note that <b>array_slice()</b> will reorder and reset the array indices by default. You can change this behaviour by setting <i><tt class="parameter">preserve_keys</tt></i> to <b><tt>TRUE</tt></b>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the slice. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.2</td> <td colspan="1" rowspan="1" align="left"> The optional <i><tt class="parameter">preserve_keys</tt></i> parameter was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>array_slice()</b> examples</b></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"><pre><div class="cdata"><pre>Array( [0] => c [1] => d)Array( [2] => c [3] => d)</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.array-splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a></li> <li class="member"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -