You could use unset() to clear parts of an array as example.
I've had multiple use-cases where I needed to delete the last entry of an array. So I reversed the array, used unset() on the last entry of the array, and reversed it again to its original sorting.
But for plain redeclaring a variable you don't need to use it. Just redeclare right off the bat.
Anther use-case could be a own written Array filter:
foreach ($array as $key => $value) {
if ($value === false){
unset($array[$key]);
}
}
Edit:
Imported the comment from @Michael:
.. and if you are dealing with non-associative arrays and depend on the index be sure to run it through array_values() or something similar. This will reset the index. Even some of the native array_* functions will not always reset it which can lead to much frustration if you do something as trivial as $myArray[count($myArray) - 1] the index might no exist anymore.
unsetin this situationunsetwhen there's a particular variable that you want to remove. That can be an array key as well. In your example, you re-created the array using the same identifier (same variable name). A good example whereunsetis used would be if you usedunset($data['red']);- your array would go down to 2 elements from 3.