unset() all?

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

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

unset() all?

Post 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?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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])
     }
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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 :oops:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Mordred wrote:Apologies to jmut and everyone :oops:
I accept... but don't let it happen again. :evil:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
{

}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It should be noted that only later versions of PHP 5 support the reference version of foreach().
Post Reply