What are the potential pitfalls of using unset() to remove elements from an array in PHP?

When using unset() to remove elements from an array in PHP, the array keys do not get reindexed automatically. This can lead to unexpected behavior when iterating over the array. To solve this issue, you can use array_values() to reindex the array after removing elements with unset().

// Original array
$array = [1, 2, 3, 4, 5];

// Remove element at index 2
unset($array[2]);

// Reindex the array
$array = array_values($array);

// Output the updated array
print_r($array);