Forum und email

array_splice

(PHP 4, PHP 5)

array_splice — 배열의 일부를 삭제하고, 그 위치에 다른 내용을 대체한다

설명

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

array_splice()input 배열로부터 offsetlength 에 정해진대로 배열원소를 제거하고, 매개변수가 주어진다면 replacement 배열의 배열원소로 그부분을 대체한다. 그리고 확장된 배열원소를 포함하는 배열을 반환한다.

offset 이 양수이면 삭제될 부분의 시작은 input 배열의 시작에서부터의 offset에서 시작된다. offset 이 음수이면 input 배열의 끝에서부터의 옵셋 수에서 시작된다.

length 가 생략되면, offset 에서 배열의 끝까지의 모든 원소를 삭제된다. length 가 주어지고 양수이면, 그 수만큼의 원소가 삭제될것이다. length 가 주어지고 음수이면, 삭제되는 부분의 끝은 배열의 끝에서부터 그 수만큼의 원소가 될것이다. 팁: replacement 가 주어져있고 offset 에서 배열 끝까지의 모든 원소를 삭제하려면 length 매개변수에 count($input)을 사용하도록 한다.

replacement 배열이 주어지면, 제거된 원소들은 이 배열의 원소들로 대체된다. offsetlength 가 아무것도 삭제되지 않도록 주어지면, replacement 배열의 원소들을 offset 에 의해 설정된 위치에 끼워넣는다. 팁: 한개의 원소만 대체되는 경우이고 그 원소가 배열자체가 아니라면 array()를 넣을 필요는 없다.

다음은 동일한 결과를 낸다:

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

제거된 원소로 구성된 배열을 반환한다.

Example#1 array_splice() 예제코드

<?php
$input 
= array ("red""green""blue""yellow");
array_splice ($input2);
// $input is now array ("red", "green")

$input = array ("red""green""blue""yellow");
array_splice ($input1, -1);
// $input is now array ("red", "yellow")

$input = array ("red""green""blue""yellow");
array_splice ($input1count($input), "orange");
// $input is now array ("red", "orange")

$input = array ("red""green""blue""yellow");
array_splice ($input, -11, array("black""maroon"));
// $input is now array ("red", "green",
//          "blue", "black", "maroon")

$input = array ("red""green""blue""yellow");
array_splice ($input30"purple");
// $input is now array ("red", "green",
//          "blue", "purple", "yellow");
?>

array_slice(), unset(), array_merge() 참고.