<HTML ><HEAD ><TITLE >array_splice</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_slice" HREF="function.array-slice.html"><LINK REL="NEXT" TITLE="array_sum" HREF="function.array-sum.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-slice.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-sum.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-splice" ></A >array_splice</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8132" ></A ><P > (PHP 4 )</P >array_splice -- Remove a portion of the array and replace it with something else </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8135" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_splice</B > ( array input, int offset [, int length [, array replacement]])<BR ></BR ><P > <B CLASS="function" >array_splice()</B > removes the elements designated by <TT CLASS="parameter" ><I >offset</I ></TT > and <TT CLASS="parameter" ><I >length</I ></TT > from the <TT CLASS="parameter" ><I >input</I ></TT > array, and replaces them with the elements of the <TT CLASS="parameter" ><I >replacement</I ></TT > array, if supplied. It returns an array containing the extracted elements. </P ><P > If <TT CLASS="parameter" ><I >offset</I ></TT > is positive then the start of removed portion is at that offset from the beginning of the <TT CLASS="parameter" ><I >input</I ></TT > array. If <TT CLASS="parameter" ><I >offset</I ></TT > is negative then it starts that far from the end of the <TT CLASS="parameter" ><I >input</I ></TT > array. </P ><P > If <TT CLASS="parameter" ><I >length</I ></TT > is omitted, removes everything from <TT CLASS="parameter" ><I >offset</I ></TT > to the end of the array. If <TT CLASS="parameter" ><I >length</I ></TT > is specified and is positive, then that many elements will be removed. If <TT CLASS="parameter" ><I >length</I ></TT > 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 <TT CLASS="parameter" ><I >offset</I ></TT > to the end of the array when <TT CLASS="parameter" ><I >replacement</I ></TT > is also specified, use <TT CLASS="literal" >count($input)</TT > for <TT CLASS="parameter" ><I >length</I ></TT >. </P ><P > If <TT CLASS="parameter" ><I >replacement</I ></TT > array is specified, then the removed elements are replaced with elements from this array. If <TT CLASS="parameter" ><I >offset</I ></TT > and <TT CLASS="parameter" ><I >length</I ></TT > are such that nothing is removed, then the elements from the <TT CLASS="parameter" ><I >replacement</I ></TT > array are inserted in the place specified by the <TT CLASS="parameter" ><I >offset</I ></TT >. Tip: if the replacement is just one element it is not necessary to put <TT CLASS="literal" >array()</TT > around it, unless the element is an array itself. </P ><P > The following equivalences hold: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >array_push ($input, $x, $y) array_splice ($input, count ($input), 0, array ($x, $y)) array_pop ($input) array_splice ($input, -1) array_shift ($input) array_splice ($input, 0, 1) array_unshift ($input, $x, $y) array_splice ($input, 0, 0, array ($x, $y)) $input[$x] = $y array_splice ($input, $x, 1, $y)</PRE ></TD ></TR ></TABLE > </P ><P > Returns the array consisting of removed elements. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8183" ></A ><P ><B >Example 1. <B CLASS="function" >array_splice()</B > examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$input = array ("red", "green", "blue", "yellow"); array_splice ($input, 2); // $input is now array ("red", "green") $input = array ("red", "green", "blue", "yellow"); array_splice ($input, 1, -1); // $input is now array ("red", "yellow") $input = array ("red", "green", "blue", "yellow"); array_splice ($input, 1, count($input), "orange"); // $input is now array ("red", "orange") $input = array ("red", "green", "blue", "yellow"); array_splice ($input, -1, 1, array("black", "maroon")); // $input is now array ("red", "green", // "blue", "black", "maroon")</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.array-slice.html" ><B CLASS="function" >array_slice()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.array-slice.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-sum.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_slice</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_sum</TD ></TR ></TABLE ></DIV ></BODY ></HTML >