Page 1 of 1
unset() all?
Posted: Wed Sep 06, 2006 5:08 am
by rsmarsha
I have variables i need to unset after they are used. They are in the form:
Code: Select all
$_POST['bodyctl_ctrlEnrollment_pages_ctrlForm:PROFILEQSTN136']
There are 4 sets of 8, each with different numbers at the end.
I can use unset() to do it one at a time, but is there a way to unset all with say 'bodyctl_ctrlEnrollment in it?
Posted: Wed Sep 06, 2006 7:04 am
by jmut
Code: Select all
//not tested, not sure if won't lead to memory leaks if unseting the index while in the foreach.(maybe store the $index to unset and then do it).
//but generally this is very weird thing to do. I think you should reconsider your design and the necessaty to unset $_POST
foreach ($_POST as $index => $value) {
if (preg_match('#^somoString#',$index)) {
unset($_POST[$index])
}
}
Posted: Wed Sep 06, 2006 10:09 am
by Chris Corbyn
Using a mutidimensional array would have made this easier since you could just rewrite the array element with an empty array.
EDIT | Duh! I mean unset() the array element.
Posted: Wed Sep 06, 2006 2:15 pm
by Mordred
You won't be able to do it as in jmut's code, as you are not allowed to change the array (only its elements) while looping with foreach.
Instead, let the foreach loop fill an array with the variables you want to unset, and then cycle this array for the actual unsetting.
Posted: Wed Sep 06, 2006 3:06 pm
by feyd
Last I checked you could unset() elements while inside a foreach().
Code: Select all
[feyd@home]>type unset.php
<?php
$arr = array(
'asdf',
'1234',
'arf',
1234,
true,
9899
);
foreach($arr as $k => $v)
{
if (is_numeric($v))
{
unset($arr[$k]);
}
}
var_dump($arr);
?>
[feyd@home]>php -r "include 'unset.php';"
array(3) {
[0]=>
string(4) "asdf"
[2]=>
string(3) "arf"
[4]=>
bool(true)
}
[feyd@home]>php -v
PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
with Xdebug v2.0.0beta6, Copyright (c) 2002, 2003, 2004, 2005, 2006, by Derick Rethans
Posted: Wed Sep 06, 2006 3:33 pm
by Mordred
feyd wrote:Last I checked you could unset() elements while inside a foreach().
I'm sorry, my bad, got it mixed up with array_walk().
There still is a possible quirk, you need to use unset($arr[$k]), not unset($v), as foreach uses a copy of the actual array.
Apologies to
jmut and everyone

Posted: Wed Sep 06, 2006 3:38 pm
by Luke
Mordred wrote:Apologies to
jmut and everyone

I accept... but don't let it happen again.

Posted: Wed Sep 06, 2006 3:46 pm
by Chris Corbyn
Mordred wrote:There still is a possible quirk, you need to use unset($arr[$k]), not unset($v), as foreach uses a copy of the actual array.
It's fair enough. Note that the following forces references but you still cannot unset() the element by unsetting the reference...
Code: Select all
foreach ($array as $key => &$value)
{
}
Posted: Wed Sep 06, 2006 3:50 pm
by feyd
It should be noted that only later versions of PHP 5 support the reference version of foreach().