Home > Backend Development > PHP Tutorial > php数组中删除元素的实现代码_PHP

php数组中删除元素的实现代码_PHP

WBOY
Release: 2016-06-01 12:12:40
Original
830 people have browsed it
复制代码 代码如下:
$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 )
?>
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template