<!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>Remove a portion of the array and replace it with something else</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-slice.html">array_slice</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-sum.html">array_sum</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-splice" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_splice</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_splice</span> — <span class="dc-title">Remove a portion of the array and replace it with something else</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-splice-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array_splice</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$input</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> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code><span class="initializer"> = array()</span></span> ]] )</div> <p class="para rdfs-comment"> Removes the elements designated by <em><code class="parameter">offset</code></em> and <em><code class="parameter">length</code></em> from the <em><code class="parameter">input</code></em> array, and replaces them with the elements of the <em><code class="parameter">replacement</code></em> array, if supplied. </p> <p class="para"> Note that numeric keys in <em><code class="parameter">input</code></em> are not preserved. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> If <em><code class="parameter">replacement</code></em> is not an array, it will be <a href="language.types.array.html#language.types.array.casting" class="link">typecast</a> to one (i.e. <code class="code">(array) $parameter</code>). This may result in unexpected behavior when using an object or <strong><code>NULL</code></strong> <em><code class="parameter">replacement</code></em>. </span> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.array-splice-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">input</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 positive then the start of removed portion is at that offset from the beginning of the <em><code class="parameter">input</code></em> array. If <em><code class="parameter">offset</code></em> is negative then it starts that far from the end of the <em><code class="parameter">input</code></em> array. </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 omitted, removes everything from <em><code class="parameter">offset</code></em> to the end of the array. If <em><code class="parameter">length</code></em> is specified and is positive, then that many elements will be removed. If <em><code class="parameter">length</code></em> is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from <em><code class="parameter">offset</code></em> to the end of the array when <em><code class="parameter">replacement</code></em> is also specified, use <em>count($input)</em> for <em><code class="parameter">length</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">replacement</code></em></span> <dd> <p class="para"> If <em><code class="parameter">replacement</code></em> array is specified, then the removed elements are replaced with elements from this array. </p> <p class="para"> If <em><code class="parameter">offset</code></em> and <em><code class="parameter">length</code></em> are such that nothing is removed, then the elements from the <em><code class="parameter">replacement</code></em> array are inserted in the place specified by the <em><code class="parameter">offset</code></em>. Note that keys in replacement array are not preserved. </p> <p class="para"> If <em><code class="parameter">replacement</code></em> is just one element it is not necessary to put <em>array()</em> around it, unless the element is an array itself, an object or <strong><code>NULL</code></strong>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-splice-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the array consisting of the extracted elements. </p> </div> <div class="refsect1 examples" id="refsect1-function.array-splice-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4983"> <p><strong>Example #1 <span class="function"><strong>array_splice()</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">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">);<br /></span><span style="color: #FF8000">// $input is now array("red", "green")<br /><br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">1</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $input is now array("red", "yellow")<br /><br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">), </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $input is now array("red", "orange")<br /><br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">1</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"black"</span><span style="color: #007700">, </span><span style="color: #DD0000">"maroon"</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">// $input is now array("red", "green",<br />// "blue", "black", "maroon")<br /><br /></span><span style="color: #0000BB">$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">"purple"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $input is now array("red", "green",<br />// "blue", "purple", "yellow");<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4984"> <p><strong>Example #2 <span class="function"><strong>array_splice()</strong></span> examples</strong></p> <div class="example-contents"><p> The following statements change the values of <var class="varname"><var class="varname">$input</var></var> the same way: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />array_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">count</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">, array(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">array_pop</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">);<br /></span><span style="color: #0000BB">array_shift</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_unshift</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_splice</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">0</span><span style="color: #007700">, array(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">$x</span><span style="color: #007700">] = </span><span style="color: #0000BB">$y</span><span style="color: #007700">; </span><span style="color: #FF8000">// for arrays where key equals offset<br /></span><span style="color: #0000BB">array_splice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-splice-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-slice.html" class="function" rel="rdfs-seeAlso">array_slice()</a> - Extract a slice of the array</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-merge.html" class="function" rel="rdfs-seeAlso">array_merge()</a> - Merge one or more arrays</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-slice.html">array_slice</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-sum.html">array_sum</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>