Questions tagged [unset]

Removing the value associated with a variable or property from the current environment.

Common usages:

  • Destroying a variable or member of an array in PHP.
  • Removing the value bound to a property in Apache Ant.
  • Removing a variable from the environment of a shell script.
595 questions
3020
votes
25 answers

Deleting an element from an array in PHP

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element? I thought that setting it to null would do it, but apparently it does not work.
Ben
  • 66,838
  • 37
  • 84
  • 108
2820
votes
11 answers

How can I remove a key from a Python dictionary?

I want to remove a key from a dictionary if it is present. I currently use this code: if key in my_dict: del my_dict[key] Without the if statement, the code will raise KeyError if the key is not present. How can I handle this more simply? See…
Tony
  • 36,591
  • 10
  • 48
  • 83
2285
votes
7 answers

How do I delete an exported environment variable?

Before installing gnuplot, I set the environment variable GNUPLOT_DRIVER_DIR = /home/gnuplot/build/src. During the installation, something went wrong. I want to remove the GNUPLOT_DRIVER_DIR environment variable. How can I achieve it?
A. K.
  • 34,395
  • 15
  • 52
  • 89
677
votes
12 answers

How can I unset a JavaScript variable?

I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script, but I don't want another script that will run later to see its value or that it was even…
Guss
  • 30,470
  • 17
  • 104
  • 128
162
votes
6 answers

How to delete object from array inside foreach loop?

I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work. foreach($array as $element) { foreach($element as $key => $value) { if($key == 'id' && $value ==…
ababa
  • 1,853
  • 4
  • 16
  • 14
106
votes
16 answers

Unset readonly variable in bash

How do I unset a readonly variable in Bash? $ readonly PI=3.14 $ unset PI bash: PI: readonly variable or is it not possible?
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
77
votes
9 answers

Unsetting array values in a foreach loop

I have a foreach loop set up to go through my array, check for a certain link, and if it finds it removes that link from the array. My code: foreach($images as $image) { if($image == 'http://i27.tinypic.com/29yk345.gif' || $image ==…
Matt
  • 1,083
  • 2
  • 10
  • 15
49
votes
3 answers

How to unset multiple variables?

Possible Duplicate: Can you unset() many variables at once in PHP? I have 3 variables var1 var2 var3. Is there's a way of un-setting them without repeated use of unset() function ?
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57
38
votes
3 answers

How unset a lot of environment variables

Have any way to unset different variables using a one command? unset HTTP_PROXY unset HTTPS_PROXY unset FTP_PROXY unset ALL_PROXY unset NO_PROXY
Bruno Wego
  • 2,099
  • 3
  • 21
  • 38
36
votes
18 answers

Problems deleting cookies, won't unset

I've tried searching the php manual and internet on how to delete cookies and I've tried it the exact same way they all say: setcookie("name", '', 1); or setcookie("name", '', time()-3600); But when I check the cookies in the cookies dialog in…
GroovyPanda
  • 3,694
  • 8
  • 31
  • 42
35
votes
4 answers

delete all keys except one in dictionary

I have a dictionary lang = {'ar':'arabic', 'ur':'urdu','en':'english'} What I want to do is to delete all the keys except one key. Suppose I want to save only en here. How can I do it ? (pythonic solution) What I have tried: In [18]: for k in…
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53
23
votes
6 answers

PHP Unset Array value effect on other indexes

I am working with a PHP loop, and I had one question regarding how unset affects the array keys. This array uses the standard numeric keys assigned by PHP, 0, 1, 2, 3 etc.... Whenever unset() runs on an array value, are the array keys shuffled or…
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
20
votes
6 answers

Fatal error: Cannot unset string offsets error?

Not sure why this is occurring: Basically, I have an array that contains the following arrays, see var_dump: array(2) { [0]=> array(1) { [0]=> string(3) "ivr" } [1]=> array(1) { [0]=> string(9) "ivr_dests" } } Obviously this…
Jared
  • 1,795
  • 6
  • 32
  • 55
18
votes
4 answers

Can you unset() many variables at once in PHP?

I have a pretty high traffic social network site, I would like to get into the habit of unsetting large array and mysql object and even some string variables. So is it possible to unset more then 1 item in PHP example:
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
17
votes
6 answers

Unset a row in a multidimensional array

I have this array $output which looks like this: Array( [0] => Array( [0] => 1a [1] => 1b [2] => 1c ) [1] => Array( [0] => 2a [1] => 2b [2] => 2c ) [2] => Array( [0] => 3a …
Giel
  • 171
  • 1
  • 1
  • 3
1
2 3
39 40