0

I was wondering if I'm in this situation :

$data = array ("red", "green", "blue");
$data = array ("yellow", "pink", "grey");
print_r($data);

what's makes the difference than this one :

$data = array ("red", "green", "blue");
unset($data);
$data = array ("yellow", "pink", "grey");
print_r($data);

it's same way to clear array. then when we should use unset()?

2
  • 2
    no need to use unset in this situation Commented Jan 22, 2016 at 9:02
  • 1
    You use unset when 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 where unset is used would be if you used unset($data['red']); - your array would go down to 2 elements from 3. Commented Jan 22, 2016 at 9:04

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

.. 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.
@Michael Imported your comment into my answer, thanks
ok cool. 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.
1

There's no difference here. unset removes the variable; use it if you want to, you know, remove a variable. Redeclaring the same variable immediately afterwards is the same as simply overwriting its contents to begin with.

Comments

1

In this case you do not need to unset because you are using it again for storing some other values.

But if you do not want to use this variable again in same code then you should unset your variable.

I personally use unset for session variables. Because if there is no use of session variables than why to store in session, Because session variables takes space in Server, it may cause server down issue.

Comments

1

In your case there is no need to use unset. But consider a situation where you don't want any key in an array which can cause application to break its flow. eg: Query builder which accepts an array of data to be inserted in to the database and when that array consists of key => value pair where key is same as column name in table..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.