CodeIgniter Forums
How to add, access, unset arrays in CI session? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to add, access, unset arrays in CI session? (/thread-31378.html)



How to add, access, unset arrays in CI session? - El Forum - 06-16-2010

[eluser]CodeIgniterNewbie[/eluser]
I want to add an array to CI session, then access it as an array (not individual elements of the array as I do not know exactly what the elements are), and later unset it. How do I do that? It seems like I can add arrays, but CI doesn't provide me with a way to access or unset arrays (of unknown elements).

Did I miss something?


How to add, access, unset arrays in CI session? - El Forum - 06-17-2010

[eluser]WanWizard[/eluser]
From our session library extension:
Code:
/**
     * Add or change data in the "userdata" array
     *
     * set_userdata method with support for multi-dimensional arrays
     *
     * @access    public
     * @param    string
     * @param    array
     * @return    void
     */
    function set_userdata_array($arrayname = NULL, $newdata = array())
    {
        if ( ! is_null($arrayname) && is_array($newdata) )
        {
            if ( !isset($this->userdata[$arrayname]) )
            {
                $this->userdata[$arrayname] = array();
            }

            if (count($newdata) > 0)
            {
                foreach ($newdata as $key => $val)
                {
                    $this->userdata[$arrayname][$key] = $val;
                }
            }
        }
    }


    /**
     * Delete a session variable from the "userdata" array
     *
     * Our version supports multi-arrays as session data
     *
     * @access    public
     * @param    string
     * @return    string
     */
    function unset_userdata($item, $subitem = NULL)
    {
        if ( is_null($subitem) )
        {
            if (is_string($item))
            {
                $item = array($item => '');
            }

            if (count($item) > 0)
            {
                foreach ($item as $key => $val)
                {
                    unset($this->userdata[$key]);
                }
            }
        }
        else
        {
            unset($this->userdata[$item][$subitem]);
        }

        $this->sess_write();
    }

    /**
     * Fetch a specific item from the session array
     *
     * Our version supports multi-arrays as session data
     *
     * @access    public
     * @param    string
     * @return    string
     */
    function userdata($item, $subitem = NULL)
    {
        if ( is_null($subitem) )
        {
            return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
        }
        else
        {
            return ( ! isset($this->userdata[$item][$subitem])) ? FALSE : $this->userdata[$item][$subitem];
        }
    }

    // --------------------------------------------------------------------

    /**
     * Delete a session variable from the "userdata" array
     *
     * Our version supports multi-arrays as session data
     *
     * @access    public
     * @param    string
     * @return    string
     */
    function unset_userdata($item, $subitem = NULL)
    {
        if ( is_null($subitem) )
        {
            if (is_string($item))
            {
                $item = array($item => '');
            }

            if (count($item) > 0)
            {
                foreach ($item as $key => $val)
                {
                    unset($this->userdata[$key]);
                }
            }
        }
        else
        {
            unset($this->userdata[$item][$subitem]);
        }

        $this->sess_write();
    }



How to add, access, unset arrays in CI session? - El Forum - 06-17-2010

[eluser]metaltapimenye[/eluser]
cowboy mode..
Code:
#...
        #array
        $x=array('a'=>'11212','b'=>'cccbcbcb');
        #session array assigning
        $this->session->set_userdata('mejik',$x);
        print_r($this->session->userdata('mejik'));
#result:Array ( [a] => 11212 [b] => cccbcbcb )

        $session=$this->session->userdata('mejik');
        #unset
        unset($session['a']);
        #re-set session
        $this->session->set_userdata('mejik',$session);
        print_r($this->session->userdata('mejik'));
#result:Array ( [b] => cccbcbcb )
#...



How to add, access, unset arrays in CI session? - El Forum - 06-17-2010

[eluser]CodeIgniterNewbie[/eluser]
Doh! It's so simple and I missed it. Here's my solution:

// To set it
$fruits = array('apple', 'banana', 'orange');
$this->session->set_userdata('fruits', $fruits);

// To get it
$my_fruits = $this->session->userdata('fruits');

// To unset it
$this->session->unset_userdata('fruits');