PHP array_splice() 함수
예시
배열에서 요소를 제거하고 새 요소로 바꿉니다.
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
array_splice($a1,0,2,$a2);
print_r($a1);
?>
정의 및 사용
array_splice() 함수는 배열에서 선택한 요소를 제거하고 새 요소로 대체합니다. 이 함수는 제거된 요소가 있는 배열도 반환합니다.
팁: 함수가 요소를 제거하지 않으면(길이=0), 대체된 배열이 시작 매개변수의 위치에서 삽입됩니다(예제 2 참조).
참고: 교체된 배열의 키는 보존되지 않습니다.
통사론
array_splice(array, start, length, array)
매개변수 값
Parameter | Description |
---|---|
array | Required. Specifies an array |
start | Required. Numeric value. Specifies where the function will start
removing elements.
0 = the first element.
If this value is set to a negative number, the function will start that far from the last element.
-2 means start at the second last element of the array. |
length | Optional. Numeric value. Specifies how many elements will
be removed, and also length of the returned array.
If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will
remove all elements, starting from the position set by the start-parameter.
|
array | Optional. Specifies an array with the elements that will be inserted to the original array. If it's only one element, it can be a string, and does not have to be an array. |
기술적 세부 사항
반환 값: | 추출된 요소로 구성된 배열을 반환합니다. |
---|---|
PHP 버전: | 4+ |
더 많은 예
실시예 1
페이지 상단의 예제와 동일한 예제이지만 출력은 반환된 배열입니다.
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
?>
실시예 2
길이 매개변수가 0으로 설정된 경우:
<?php
$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);
print_r($a1);
?>
❮ PHP 배열 참조