How to properly delete an element from an array in PHP? Problems with unset and reindexing
Hello everyone! I'm a beginner in PHP and encountered a problem when deleting elements from an array. I'm writing a simple script for managing a task list. I have an array with tasks, and I want to delete a task by its index.
Here's my code:
```php
$tasks = ['Buy milk', 'Do homework', 'Call a friend', 'Brush teeth'];
// I want to delete the task with index 1 ('Do homework')
unset($tasks[1]);
print_r($tasks);
```
After executing unset, I get this output:
```
Array
(
[0] => Buy milk
[2] => Call a friend
[3] => Brush teeth
)
```
Index 1 is gone, but the remaining indices are not reorganized! Now I have a gap in the array. When I try to access the element at index 1, I get an error. And if I want the array to be sequential (0, 1, 2), how do I do that?