papaface Posted December 30, 2007 Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 30, 2007 Share Posted December 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426180 Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 the best way to do this is simply this <?php if(count($_SESSION['lerror'])){$_SESSION['lerror'] = array();} ?> Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426182 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 When I do both of those it doesn't echo anything out on the page: if (count($_SESSION['lerror']) > 0) { foreach ($_SESSION['lerror'] as $key => $value) { echo $value . "<br />"; } unset($_SESSION['lerror']); } Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426183 Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 This helps http://us2.php.net/manual/en/function.unset.php search for $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426184 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 But I don't want to unset the entire $_SESSION because I have other data in that. I just want to remove the entire $_SESSION['lerror'] array. Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426186 Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426188 Share on other sites More sharing options...
PHP_PhREEEk Posted December 30, 2007 Share Posted December 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426189 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 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. ) Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426193 Share on other sites More sharing options...
papaface Posted December 30, 2007 Author Share Posted December 30, 2007 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. ??? Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426233 Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426241 Share on other sites More sharing options...
papaface Posted December 31, 2007 Author Share Posted December 31, 2007 Works, thanks Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426269 Share on other sites More sharing options...
cooldude832 Posted December 31, 2007 Share Posted December 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83755-solved-unset-doing-something-weird-i-think/#findComment-426272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.