Jump to content

[SOLVED] Unset Doing Something Weird... I think


Recommended Posts

Hello,

I have the following code:

if	(count($_SESSION['lerror']) >= 1)
{
foreach	($_SESSION['lerror'] as $key => $value)
	{
	echo $value . "<br />";
	unset($_SESSION['lerror'][$key]);
	}
}

I have a multidimensional array to show me errors.

I want to echo those errors found in the $_SESSION['lerror'] array.

Once they have been echo'ed, I want to remove that key from the array.

With the code below, it echo's one error out and then unsets ALL of the other elements in the array. When I comment out the

unset($_SESSION['lerror'][$key]);

Line, the script shows me all the necessary errors but does not remove the keys from the session, therefore the errors always show.

 

Why is unset functioning like this?

First:

 

<?php
if ( count($_SESSION['lerror']) >= 1 )

 

can be simplified to:

 

<?php
if ( count($_SESSION['lerror']) > 0 )

 

as anything greater than 0 is also greater than or equal to 1.

 

Onto your question, you say that you want to remove individual keys that are already echo'd, yet your code does not do this. It basically attempts to empty the entire array if any elements exist in it. Therefore, just simply unsetting the array should do the same thing, without a foreach loop being necessary.

 

<?php
if ( count($_SESSION['lerror']) > 0 ) {
    unset($_SESSION['lerror']);
}

 

PhREEEk

did u try what I gave you? it should solve it you can't unset a superglobal because if u read what it said in that article

"Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal."

 

Just try what I gave you and tell me if it works

Also...

 

If you are used to 'testing' the error array even if it's empty, you might get a notice error for an undefined index or undefined variable. You would only see this if you had error notices set to show... so, to solve that, you could do this:

 

<?php
if ( is_array($_SESSION['lerror'] && !empty($_SESSION['lerror']) ) ) {
   $_SESSION['lerror']) = array();
}

 

Which will accomplish the same thing, but the array will remain declared (it will just be emptied).

 

PS. Posted after cooldude's post, but left anyways to explain why you'd want to do something like this.

 

PhREEEk

Okay this is what I have at the moment.

if	(is_array($_SESSION['lerror']))
{
foreach	($_SESSION['lerror'] as $key => $value)
	{
	echo $value . "<br />";
	}
}

 

That works FINE, no problems at all.

The problem is when I want to remove the $_SESSION['lerror'] array.

So I do:

if	(is_array($_SESSION['lerror']))
{
foreach	($_SESSION['lerror'] as $key => $value)
	{
	echo $value . "<br />";
	}
$_SESSION['lerror'] = NULL;
}

But now no errors are displayed and when the $_SESSION['lerror'] array is supposed to be created, it doesn't...

 

Then when I try:

if	(is_array($_SESSION['lerror'] && !empty($_SESSION['lerror'])))
{
foreach	($_SESSION['lerror'] as $key => $value)
	{
	echo $value . "<br />";
	}
$_SESSION['lerror'] = NULL;
}

The errors are created in the session HOWEVER they do not echo out on the page yet at the bottom when i print_r($_SESSION['lerror']) I get:

Array ( [0] => You have entered an incorrect password. Please try again. ) 

I've found out a way to make it work.

This works:

<?php
//if	(is_array($_SESSION['lerror'] && !empty($_SESSION['lerror'])))
//{
foreach	($_SESSION['lerror'] as $key => $value)
	{
	echo $value . "<br />";
	}
$_SESSION['lerror'] = NULL;
//	}

But if you remove the comments slashes, it doesn't. ???

do u got session_start(); above it?

if so try

<?php
if(!empty($_SESSION['lerror'])){
   if(is_array($_SESSION['lerror'])){
        foreach($_SESSION['lerror'] as $value){
              echo $value."<Br />";
         }
   }
   $_SESSION['lerror'] = NULL;
}
?>

 

If it errors let me know

if your  error reporting is way up there you have to do this sorta of top down checking because first you can't check if somethign is_array if it is empty so that will error and you can't do a foreach if its not an array so that too will error

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.