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

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageGerman 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_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
Sat, 12 Aug 2000
array_unshift 
Next page


array_splice

(PHP4 )

array_splice -- 配列の一部を削除し、他の要素で置換する

説明

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

array_splice()は、配列input からoffsetおよびlengthで 指定された要素を削除し、配列replacementが 指定されたこの要素で置換します。

offsetが正の場合、削除される部分は 配列inputの最初から指定オフセットの位置 からとなります。 offsetが負の場合、削除される部分は、 inputの末尾から数えた位置からとなります。

lengthが省略された場合、 offsetから配列の最後までが全て削除されます。 lengthが指定され、正の場合、複数の要素が 削除されます。 lengthが指定され、負の場合、削除される 部分は配列の末尾から複数の要素となります。 ヒント: replacementも指定した場合に offsetから配列の最後まで全てを削除するには、 lengthを求めるためにcount($input) を使用して下さい。

配列replacementが指定された場合、 削除された要素は、この配列の要素で置換されます。 offsetおよびlengthで 何も削除しないと指定した場合、配列replacementの 要素はoffsetで指定された位置に挿入されます。 ヒント:replacementに一つしか要素がない場合、要素そのものが配列でない限り、 array()で括る必要はありません。

以下は等価となります。

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)
     

削除された要素からなる配列を返します。

例 1. array_splice()の例


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

array_splice($input, 2);      // $input は、array("red", "green") となります。
array_splice($input, 1, -1);  // $input は、array("red", "yellow") となります。
array_splice($input, 1, count($input), "orange");  
                              // $input は、array("red", "orange") となります。
array_splice($input, -1, 1, array("black", "maroon")); 
                              // $input は、array("red", "green", 
                              //    "blue", "black", "maroon") となります。
      

array_slice()も参照下さい。

注意: この関数は、PHP 4.0で追加されました。


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
Sat, 12 Aug 2000
array_unshift 
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.