zend-frameworkzend-session-namespace

Zend Session Namespace, get key of defined array


I'm using some Zend_Session_Namespace objects to save some data, most of these data or multidimensional arrays. I have the feeling that I miss a part of the Zend_Session_Namespace logic or I don't use them for what they are supposed to do.

For example I store a cd id with its tracklist as an array.

$session = new Zend_Session_Namespace('cd-track-list');
$session->{123} = array('First Track', 'Second Track');

Afterwards I want to loop the session namespace and I want to know the id of the cd and its tracklist.

foreach($session as $key => $value {
   // $key is 0 -> but should be 123
   // $value is array('First Track', 'Second Track');
}

The $value is correct, but the $key isn't set here. So my question is, how can I get the id of the cd when I loop all cd's?

Thanks in advance.


Solution

  • It will not work with your Integer index, it is casted to "0" thats the problem. See "Zend_Session_Namespace.php" (__set)

    $name = (string) $name;

    You can use:

    $session = new Zend_Session_Namespace('cd-track-list');
    $session->a = array('First Track', 'Second Track');
    
    foreach($session as $key => $value {
       // $key is 0 -> but should be 123
       // $value is array('First Track', 'Second Track');
    }