-
- $arr = array('a','b','c','d');
- unset($arr[1]);
- print_r($arr);
- ?>
Copy the code
After using unset before, the array $arr should compress the array to fill the missing element positions, but after print_r($arr), the final result is Array ( [0] => a [2] => c [3] => d );
Let’s take a look at the form of a digital array:
-
- $arr = range(5,10,4);
- print_r($arr);//Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 10 )< /span>
- unset($arr[1]);//Array ( [0] => 5 [2] => ; 7 [3] => 8 [4] => 9 [5] => 10 )
- print_r($arr);
- ?>
Copy the code to see The output form is also an array that will fill in the positions of missing elements.
So how can we ensure that missing elements are filled in and the array is re-indexed?
Use: array_splice():
Example:
$arr = array('a','b','c','d'); array_splice($arr,1,1);
print_r($arr) ; // Array ( [0] => a [1] => c [2] => d )< /span>
?>
Copy code
|