(PHP 4, PHP 5, PHP 7)
array_splice — 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체
input
배열에서 offset
과
length
로 정해진 대로 배열원소를 제거하고, 인수가
주어진다면 replacement
배열의 원소로 그 부분을
대체한다.
input
의 숫자 키는 유지되지 않습니다.
Note:
replacement
가 배열이 아니면, 자료형 변환이 일어납니다. (즉,(array) $parameter
) 이로 인해replacement
에 객체를 사용할 때, 결과를 예측할 수 없습니다.
input
입력 배열.
offset
offset
이 양수이면 삭제될 부분의 시작은
input
배열의 시작에서부터의 offset에서 시작된다.
offset
이 음수이면
input
배열의 끝에서부터의 옵셋 수에서 시작된다.
length
length
가 생략되면, offset
에서
배열의 끝까지의 모든 원소를 삭제된다. length
가
주어지고 양수이면, 그 수만큼의 원소가 삭제될것이다.
length
가 주어지고 음수이면, 삭제되는 부분의 끝은
배열의 끝에서부터 그 수만큼의 원소가 될것이다.
팁: replacement
가 주어져있고
offset
에서 배열 끝까지의 모든 원소를 삭제하려면
length
인수에 count($input)을
사용하도록 한다.
replacement
replacement
배열이 주어지면, 제거된 원소들은 이
배열의 원소들로 대체된다.
offset
과
length
가 아무것도 삭제되지 않도록 주어지면,
replacement
배열의 원소들을
offset
에 의해 설정된 위치에 끼워넣는다.
replacement 배열의 키는 유지되지 않습니다.
replacement
가 하나의 원소이고, 그 원소가 배열
자체가 아니라면 array()를 넣을 필요는 없다.
추출된 원소로 구성된 배열을 반환한다.
Example #1 array_splice() 예제
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");
?>
Example #2 array_splice() 예제
다음 구분은 $input의 값을 동일한 방법으로 변경합니다:
<?php
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));
$input[$x] = $y; // for arrays where key equals offset
array_splice($input, $x, 1, $y);
?>