php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #48944 unsetting an array does not reindex
Submitted: 2009-07-16 13:11 UTC Modified: 2009-07-16 14:00 UTC
From: info at speich dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2009-07-16 13:11 UTC] info at speich dot net
Description:
------------
I think it would be helpful if the documentation about unset would mention:

Unsetting an array item removes it from the array, but does not change the keys. Example:
<?php
$arr = array(5, 6, 7, 8);
print_r($arr);
unset($arr[3]);
print_r($arr);
?>
Output:
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )
Array ( [0] => 5 [1] => 6 [2] => 7 )


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-16 13:14 UTC] info at speich dot net
Sorry, better example:
<?php
$arr = array(5, 6, 7, 8);
print_r($arr);
unset($arr[2]);
print_r($arr);
?>
Output:
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )
Array ( [0] => 5 [1] => 6 [3] => 8 )
 [2009-07-16 14:00 UTC] rquadling@php.net
http://docs.php.net/manual/en/language.types.array.php#language.types.ar
ray.useful-funcs

Note: The unset() function allows removing keys from an array. Be aware 
that the array will not be reindexed. If a true "remove and shift" 
behavior is desired, the array can be reindexed using the array_values() 
function. 
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Oct 09 00:00:01 2026 UTC