Jump to content

[SOLVED] array_pop equivalent for SESSION


pee_aych_pee

Recommended Posts

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

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.

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

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

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.