array_splice

(PHP 4 )

array_splice --  배열의 일부를 삭제하고, 그 위치에 다른 내용을 끼워 넣는다.

설명

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

array_splice()input 배열로 부터 offsetlength로 정해진 엘린먼트를 삭제하고, replacement배열이 제공된다면 이를 제공된 배열으리 엘리먼트로 대체시킨다.

만약 offset이 양의 값이라면, 삭제 부분의 시작은 input 배열의 처음 부터 그 해당하는 옵셋까지이다. 만약 offset이 음의 값이라면, input 배열의 끝에서 부터 옵셋만큼 떨어진 곳에서 부터 시작된다.

만약 length이 생략되면, offset에서부터 배열의 끝까지의 모든 엘리먼트를 삭제한다. 만약 length가 정의되고 양의 값을 갖는다면, 그 수 만큼의 엘리먼트가 삭제된다. length가 정의되고 음의 값을 갖는다면, 삭제되는 부분의 끝이 배열의 끝에서부터의 숫자가 된다. 팁: replacement가 지정되어 있을 때, offset에서부터 배열의 끝까지의 모든 엘리먼트를 삭제하려면, length대신 count($input)을 사용하라.

replacement 배열이 지정되어 있으면, 삭제된 엘리먼트는 이 배열의 엘리먼트로 대체된다. 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.

예 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().