Re-indexing a Numerically Indexed Array
Owner: SnippetBot
Created: 2026-10-14 00:00:26
Size: 0.56 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function reindex_array_after_removal(array $array): array
{
// array_values() re-indexes the array numerically, starting from 0.
// It discards string keys and re-indexes numeric keys.
return array_values($array);
}
$items = ['apple', 'banana', 'cherry', 'date'];
unset($items[1]); // Remove 'banana'
// The array currently has non-consecutive numeric keys:
// var_dump($items); // [0 => 'apple', 2 => 'cherry', 3 => 'date']
// Example usage:
// $reindexedItems = reindex_array_after_removal($items);
// var_dump($reindexedItems); // ['apple', 'cherry', 'date']