We have many ways to delete an array element in PHP, but the most commonly used one is to use the unset function. But how do we rebuild the array index after deleting the middle array element? Please refer to it below.
The code is as follows
代码如下 |
复制代码 |
$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
)
**/
|
|
Copy code |
|
$arr = array(1,2,3,4);
unset($arr[1]);
echo $array[1]; // error Undefined offset
print_r($arr);
//The output is as follows
/**
Array
(
[0] => 1
[2] => 3
[3] => 4
)
**/
$arr = array_values($arr);
print_r($arr);
//The output is as follows
/**
Array
(
[0] => 1
[1] => 3
[2] => 4
)
**/
http://www.bkjia.com/PHPjc/628902.htmlwww.bkjia.com
trueTechArticleWe have many ways to delete an array element in php, but the most commonly used one is to use the unset function, but delete How do we rebuild the array index if the middle array element is missing? Next...