★ wanayoo — archive 1999 http://www.php.net/manual/function.array-splice.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: array_splice
View the source code for this pageSearch the site



Previous page
 array_slice
 Updated
Mon, 07 Aug 2000
array_unique 
Next page


array_splice

(PHP4 )

array_splice --  Remove a portion of the array and replace it with something else

Description

array array_splice (array input, int offset [, int length [, array replacement]])

Array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.

If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.

If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length 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 offset to the end of the array when replacement is also specified, use count($input) for length.

If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Tip: if the replacement is just one element it is not necessary to put array() around it, unless the element is an array itself.

The following equivalences hold:

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))
$a[$x] = $y                     array_splice ($input, $x, 1, $y)
     

Returns the array consisting of removed elements.

Example 1. Array_splice() examples


$input = array ("red", "green", "blue", "yellow");

array_splice ($input, 2);      // $input is now array ("red", "green")
array_splice ($input, 1, -1);  // $input is now array ("red", "yellow")
array_splice ($input, 1, count($input), "orange");  
                               // $input is now array ("red", "orange")
array_splice ($input, -1, 1, array("black", "maroon")); 
                               // $input is now array ("red", "green", 
                               //          "blue", "black", "maroon")
      

See also array_slice().


User Contributed Notes: array_splice


siffert@museworld.com
10-Apr-2000 12:16
Spent an hour trying to figure out how to remove an element from an *associative* array. splice won't do it and there are no "delete" methods either. setting the key to null doesn't shrink the size of the array. I don't think it's possible. pain.


andrei@ispi.net
28-Apr-2000 07:24
A little hacking perhaps, but here goes the solution to the abovementioned problem of being unable to remove an element by key.

<pre>$key_index = array_keys(array_keys($array), $target_key);
array_splice($array, $key_index[0], 1);
</pre>



espie@online.no
14-Jul-2000 04:19
Re: A little hacking perhaps


I tried this, and it worked just fine! But then I noticed that I had lost my original keys; they became ordinary index-numbers :(
Suggestions?



ricky@ea1.com
14-Jul-2000 06:56
I've got The Answer: use unset(). i.e.

<pre>
$example = array (
"a" => "alpha",
"b" => "beta",
"d" => "delta",
"g" => "gamma"
);

print_r($example);
</pre>gives:

Array ( [a] => alpha [b] => beta [d] => delta [g] => gamma )

Now use unset()
<pre>
unset($example['d']);
print_r($example);
</pre>
gives:

Array ( [a] => alpha [b] => beta [g] => gamma )


I came across this by accident; I can't believe they didn't note this here.



clmclny@mta.ca
25-Jul-2000 03:23
It should be noted somewhere what array_splice returns. The prototype says that it returns an array and this would seem to imply that it returns the modified input array. But after much frustration wondering why my script wasn't working, I finally realized that array_splice returns the portion of the input array that it cuts out, and *not* the modified array.


 About Notes


Previous page
 array_slice
 Updated
Mon, 07 Aug 2000
array_unique 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.