Home > php教程 > php手册 > PHP中利用unset()函数删除数组后重建索引

PHP中利用unset()函数删除数组后重建索引

WBOY
Release: 2016-06-13 10:15:55
Original
1065 people have browsed it

在php中删除一个数组元素我们有很多种方法,但是常用的就是利用unset函数了,但是删除中间的数组元素了我们要怎么重建数组索引呢,下面来参考。

 代码如下 复制代码

$arr = array(1,2,3,4);
unset($arr[1]);
echo $array[1]; // error Undefined offset
print_r($arr);

// 输出如下
/**
Array
(
    [0] => 1
    [2] => 3
    [3] => 4
)
**/
$arr = array_values($arr);
print_r($arr);
// 输出如下
/**
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
)
**/

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