之前如果要在某个数组中删除一个元素,我是直接用的unset,也不管unset之后会发生什么。但今天看到的东西却让我大吃一惊
代码如下:
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?>
print_r($arr)之后,结果却不是那样的,最终结果是 Array ( [0] => a [2] => c [3] => d
那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice():
代码如下:
$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr); //
Array ( [0] => a [1] => c [2] => d ) ?>
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn