Home > php教程 > php手册 > 如何删除PHP数组中的元素(unset,array_splice)?

如何删除PHP数组中的元素(unset,array_splice)?

WBOY
Release: 2016-06-13 09:23:57
Original
2170 people have browsed it

如何删除PHP数组中的元素(unset,array_splice)?

如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排:

<?php  
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
Copy after login

结果是:

Array ( [0] => a [2] => c [3] => d )
Copy after login

那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice()

<?php  
$arr = array('a','b','c','d');  
array_splice($arr,1,1);  
print_r($arr);
Copy after login

结果是:

Array ( [0] => a [1] => c [2] => d )
Copy after login

您可能感兴趣的文章

  • PHP去除数组中的空值元素(array_filter)
  • php在数组中查找某个值是否存在(in_array(),array_search(),array_key_exists())
  • php如何删除数组的第一个元素和最后一个元素
  • JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
  • PHP合并数组+与array_merge的区别
  • php清除数组中的空值元素
  • PHP数组函数array_walk()笔记
  • php获取数组的最后一个元素
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template