'unset' 함수를 사용하여 배열에서 요소를 제거하고 배열의 인덱스를 재설정하는 'array_values' 함수를 사용할 수 있습니다.
예시
<?php $my_arr = array( 'this', 'is', 'a', 'sample', 'only'); echo"The array is "; var_dump($my_arr); unset($my_arr[4]); echo"The array is now "; $my_arr_2 = array_values($my_arr); var_dump($my_arr_2); ?>
출력
The array is array(5) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
[4]=>
string(4) "only"
}
The array is now array(4) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
} 문자열 값을 포함하는 배열이 선언되었습니다. 배열이 표시되고 'unset' 기능을 사용하여 배열에서 특정 인덱스 요소를 삭제합니다. 그런 다음 콘솔에 변경 사항을 반영하기 위해 어레이가 다시 표시됩니다.