pee_aych_pee Posted May 13, 2007 Share Posted May 13, 2007 PHP Version 5.2.1 XAMPP for Windows Version 1.6.0a I understand how array_push() and array_pop() work on simple arrays, but I need something like this to manage $_SESSION arrays. So far, the best I can come up with is using unset() unset($_SESSION[$key]); But, I would like to use the push/pop functionality for $_SESSION arrays. I guess it is as easy as writing my own function to simulate it, but it would be nice to harness this power if it already exists in a predefined function. I scoured the us.php.net website, and to no avail. Sessions don't seem to be equipped with this. Or, perhaps I am doing it wrong. Any ideas? Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/ Share on other sites More sharing options...
trq Posted May 13, 2007 Share Posted May 13, 2007 $_SESSION arrays are no different to any other associative array. Or, perhaps I am doing it wrong. Perhaps you are, can we see some code? Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-251813 Share on other sites More sharing options...
pee_aych_pee Posted May 13, 2007 Author Share Posted May 13, 2007 Thanks for the quick response...and sorry for my delay. It was late. Yes, I have an example I've been working on. I have mysql populate a table, and according to the column the user selects he/she can filter the data more efficiently. So, the column names are table names in the mysql db. <?php // SESSION DYNAMICALLY LOADED, BUT I'VE SIMPLIFIED IT FOR THE FORUM $_SESSION['filterfield'][0] = 'last_name'; $_SESSION['filterfield'][1] = 'first_name'; $_SEESSION['filterfield'][2] = 'city'; // REMOVE ONE OF THE INDICIES $_SESSION['filterfield'] = array_pop($_SESSION['filterfield']); // GET SIZE $sizeofarray = sizeof($_SESSION['filterfield']); // ECHO echo $sizeofarray; ?> I don't get any output. Thanks for your help. I am using array_push() also, and it's not working either. Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-251984 Share on other sites More sharing options...
kenrbnsn Posted May 13, 2007 Share Posted May 13, 2007 You are used the function array_pop() incorrectly in this line: <?php $_SESSION['filterfield'] = array_pop($_SESSION['filterfield']); ?> This line tells PHP to replace your array "$_SESSION['filterfield']" with the element that was popped off of the array. What you probably want to do is <?php $last_element = array_pop($_SESSION['filterfield']); ?> Ken Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-251993 Share on other sites More sharing options...
Barand Posted May 13, 2007 Share Posted May 13, 2007 Not a good idea to assign the value popped to the same array. Have you called session_start() ? <?php session_start(); // SESSION DYNAMICALLY LOADED, BUT I'VE SIMPLIFIED IT FOR THE FORUM $_SESSION['filterfield'][0] = 'last_name'; $_SESSION['filterfield'][1] = 'first_name'; $_SESSION['filterfield'][2] = 'city'; // REMOVE ONE OF THE INDICIES $filterfield = array_pop($_SESSION['filterfield']); //--> city // GET SIZE $sizeofarray = sizeof($_SESSION['filterfield']); // ECHO echo $sizeofarray; //--> 2 ?> Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-252001 Share on other sites More sharing options...
pee_aych_pee Posted May 13, 2007 Author Share Posted May 13, 2007 Thanks a bunch - it seems as though this is resolved via your answers. Now I understand what I did wrong. I mistakenly assumed the value to the left of the assignment operator was the resulting array with the last index removed. Which is totally wrong. <?php // WRONG $_SESSION['filterfield'] = array_pop($_SESSION['filterfield']); ?> The wrong way gave me the opposite result I was expecting! The value to the right of the assignment operator is the removed index. Silly me. <?php // RIGHT $popped = array_pop($_SESSION['filterfield']); ?> Thanks kenrbnsn, I didn't fully understand the proper usage of array_pop(). And Barand, you're right, but I only left out the session_start() for brevity. It's in my actual code. In any event, I searched many forums and websites with no coverage of this topic. The local bookstore had lots of good books in stock, but none of them mentioned the behavior of session arrays. Thanks again. Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-252344 Share on other sites More sharing options...
kenrbnsn Posted May 13, 2007 Share Posted May 13, 2007 The best source of information (IMHO) is the on-line PHP manual. You can always go directly to the manual page of a function by using this formate for your URL:http://www.php.net/name_of_function Ken Link to comment https://forums.phpfreaks.com/topic/51148-solved-array_pop-equivalent-for-session/#findComment-252408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.