PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
rsmarsha
Forum Contributor
Posts: 242 Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England
Post
by rsmarsha » Wed Sep 06, 2006 5:08 am
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?
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Wed Sep 06, 2006 7:04 am
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])
}
}
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed Sep 06, 2006 10:09 am
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.
Mordred
DevNet Resident
Posts: 1579 Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria
Post
by Mordred » Wed Sep 06, 2006 2:15 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 06, 2006 3:06 pm
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
Mordred
DevNet Resident
Posts: 1579 Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria
Post
by Mordred » Wed Sep 06, 2006 3:33 pm
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
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Wed Sep 06, 2006 3:38 pm
Mordred wrote: Apologies to
jmut and everyone
I accept... but don't let it happen again.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed Sep 06, 2006 3:46 pm
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)
{
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 06, 2006 3:50 pm
It should be noted that only later versions of PHP 5 support the reference version of foreach().