Let’s give an example:
Copy code The code is as follows:
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?>
I previously imagined that after unset, the array $arr should compress the array to fill in the missing element positions, but after print_r($arr), the result is not like that. The final result is Array ( [0] => a [2] => c [3] => d );
If this is the case, then let’s take a look at the form of a digital array
Copy code The code is as follows:
$arr = range(5,10,4) ;
print_r($arr);//Array ( [0] => 5 [1] => 6 [ 2] => 7 [3] => 8 [4] => 9 [5] => 10 )
unset($arr[1]);//Array ( [0] => 5 [2] => 7 [3] => 8 [4] => 9 [5 ] => 10 )
print_r($arr);
?>
You can see that the output is in the form of an array, which will fill in the missing elements. Location. So how can we ensure that missing elements are filled in and the array is re-indexed? The answer is array_splice():
Copy code The code is as follows:
$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr) ; // Array ( [0] => a [1] => c [2] => d )< /span>
?>
http://www.bkjia.com/PHPjc/744325.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/744325.htmlTechArticleLet’s give an example: Copy the code as follows: ?php $arr = array('a','b ','c','d'); unset($arr[1]); print_r($arr); ? What I imagined before was that after unset, the array $arr should be compressed...