PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




array_splice

Name

array_splice — Replaces part of an array with another array.

Synopsis

array array_splice(array, offset[, length][, replacement]);
array array: Array to act on
int offset: Starting element index
int length (optional): Length of segment to replace
array replacement (optional): Array of replacement elements

Returns

Array of elements deleted from the original array; NULL on failure

Description

This function deletes the elements of array starting at the position given by offset . If offset is positive, it gives the number of elements from the beginning of array from which to start deleting; if negative, it gives the number of elements from the end at which to start deleting.

array is altered in place, and an array containing the deleted elements is returned.

If length is given and is positive, it gives the number of elements to delete. If negative, it gives the number of elements from the end of array at which to stop deleting. If not given, all elements from offset to the end of array are deleted.

If replacement is given, the deleted elements of array are replaced with the contents of replacement . You can use this to cause replacement to simply be inserted into array with no deletions taking place by setting length to 0.

Version

PHP 4

Example

Example 33. Splice arrays together

$arr1 = array("black", "white", "eggplant", "grey");
$arr2 = array("red", "blue", "green");
echo "Original arrays: ", implode(", ", $arr1), " & ", implode(", ", $arr2), "\n";
$items = array_splice($arr1, 1, 2, $arr2);
echo "New array: ", implode(", ", $arr1), "\n";
echo "Parts removed: ", implode(", ", $items);

Output:
Original arrays: black, white, eggplant, grey & red, blue, green
New array: black, red, blue, green, grey
Parts removed: white, eggplant



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.