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
17
votes
3 answers

Unset an array element inside a foreach loop

I'm accessing an array by reference inside a foreach loop, but the unset() function doesn't seem to be working: foreach ( $this->result['list'] as &$row ) { if ($this_row_is_boring) { unset($row); } } print_r($this->result['list']);…
Summer
  • 2,488
  • 3
  • 23
  • 32
16
votes
4 answers

Unset all variables in PHP script

Trying to unset automatically all variables in script. Have tried this way: echo '
Variables in Script before unset():
'; print_r(array_keys(get_defined_vars())); echo '

'; var_dump(get_defined_vars()); // Creates…
Ash501
  • 321
  • 2
  • 4
  • 10
14
votes
2 answers

How to remove column from child collection

I have a collection in MongoDB called CrawlUser. It has a list called CrawlStatuses, which is a list of CrawlStatus objects. CrawlStatus has a property called LastErrorMessage which I want to remove from the collections. I tried to do the following…
Justin
  • 17,670
  • 38
  • 132
  • 201
14
votes
4 answers

Parse error: syntax error, unexpected 'unset' (T_UNSET)

I am using simple php unset() function to remove and index of array but it's showing the following error: Parse error: syntax error, unexpected 'unset' (T_UNSET) Here is my erroneous code: echo $totalArray = unset($linkExtHelp[0]); Thanks in…
Khandad Niazi
  • 2,326
  • 3
  • 25
  • 22
12
votes
3 answers

Are variables used in PHP functions automatically unset after function execution?

I have a question regarding the variables/arrays used in PHP functions. After executing the function, are all the variables automatically unset? If not, when do they unset exactly, after executing the whole PHP page? After a certain time? Is it…
Martin
  • 123
  • 1
  • 4
12
votes
4 answers

unset a element of an array via reference

I can access anywhere inside the multi-dimensional an array via reference method. And I can change the its value. For example: $conf = array( 'type' => 'mysql', 'conf' => array( 'name' => 'mydatabase', 'user' =>…
eburhan
  • 123
  • 1
  • 5
12
votes
5 answers

What is the best method for memory cleanup in PHP? (5.2)

I have two simple questions. What is better/useful for memory cleanup. $var = null; or unset($var); I have one function with one cycle. I am getting (after few minutes) Fatal error: Allowed memory size of 419430400 bytes exhausted I am setting…
Michal Drozd
  • 1,311
  • 5
  • 13
  • 26
11
votes
5 answers

Does unsetting array values during iterating save on memory?

This is a simple programming question, coming from my lack of knowledge of how PHP handles array copying and unsetting during a foreach loop. It's like this, I have an array that comes to me from an outside source formatted in a way I want to…
Soulriser
  • 419
  • 8
  • 16
11
votes
5 answers

unset variable in php

I just read about unset variable through php manual. The php manual says "unset() destroys the specified variables" This def seems perfect until I came across static variable... "If a static variable is unset() inside of a function, unset()…
Tarun
  • 3,162
  • 3
  • 29
  • 45
10
votes
7 answers

php - unset $this

I've made a class that acts like an file wrapper. When user call delete method, i want to unset the object (actually $this). Is there a way (workaround) to do that? The manual said no, there is no way...
thomas
  • 269
  • 2
  • 4
  • 6
10
votes
10 answers

PHP unset vs array_pop?

If I want to remove the last element of an array, I can use either of these two code: array_pop($array); (the return value is not used) unset($array[count($array) -1]); Is there any performance or semantic difference between them? If not, which is…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
9
votes
1 answer

Making a nameref a regular variable in bash

EDIT: This has been confirmed to be a bug and will be fixed: https://lists.gnu.org/archive/html/bug-bash/2018-03/msg00055.html So I'm messing around with bash's indirection feature, namerefs. I thought I had gotten the hang of it, but then I hit…
greatBigDot
  • 505
  • 3
  • 9
9
votes
7 answers

PHP | Remove element from array with reordering?

How can I remove an element of an array, and reorder afterwards, without having an empty element in the array? 12,1=>32 ); unset($c[0]); // will distort the array. ?> Answer / Solution: array array_values ( array $input…
19h
  • 819
  • 9
  • 20
9
votes
1 answer

Magento: Block unsetChild for all children?

I am aware of: as Is it possible to remove all children in one line? e.g: * Or something similar..? UPDATE - ANSWER:
user76568
  • 456
  • 4
  • 16
9
votes
1 answer

How do I check if a variable is unset AND use set -u in my bash prelude?

I know that set -u will fail the script if there are any unbound variables referenced, but in my bash script, I'm checking to see if a certain variable is unset within an if statement before I attempt to do something with it, like so: if [[ -z…
3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
1
2
3
39 40