WordPress.org

Ready to get started?Download WordPress

Forums

WP Session Manager
[resolved] Store an array, and get back an object? (5 posts)

  1. Jason Judge
    Member
    Posted 1 year ago #

    On one page I store an array (from a form submission) which looks like this:

    Array
    (
        [id] => 8
        [form_id] => 1
        [date_created] => 2013-06-15 20:47:48
        [is_starred] => 0
        [is_read] => 0
    )

    On the next page I fetch the array out of the session and it now looks like this:

    Recursive_ArrayAccess Object
    (
        [container:protected] => Array
            (
                [id] => 10
                [form_id] => 1
                [date_created] => 2013-06-15 21:26:42
                [is_starred] => 0
                [is_read] => 0
            )
    )

    I can still access this object like an array, but just wondering what the reason for this apparent change is.

    -- Jason

    http://wordpress.org/extend/plugins/wp-session-manager/

  2. Jason Judge
    Member
    Posted 1 year ago #

    Actually, I can access elements of the array, but I cannot iterate over it. I need to be able to get the same data out of the session as I put in. Do I need to serialize data before it goes in?

    I've tried using json_encode() and json_decode() to translate my data between arrays and strings, and that cleanly gives me the correct data back when retrieving from the session. The documentation does suggest that I should not have to do that, however.

  3. Eric Mann
    Member
    Plugin Author

    Posted 1 year ago #

    Internally, the session uses nested arrays to store data. So your array lives inside an internal, hidden array. The Recursive_ArrayAccess object is merely an object that implements the same interfaces that Arrays implement, but which gives us the ability to have arbitrarily nested associative and non-associative arrays in the collection.

    Rather than implement all of the other Array interfaces for Recursive_ArrayAccess (i.e. Iterable, which is implemented for WP_Session itself), I extended a toArray() method on the Recursive_ArrayAccess object.

    So instead of:

    $data = $wp_session['data'];

    You'd use:

    $data = $wp_session['data']->toArray();

    This method returns an Array, and you can iterate over it however you want.

  4. Jason Judge
    Member
    Posted 1 year ago #

    Thanks, I'll give that a go.

  5. eugenealegiojo
    Member
    Posted 5 months ago #

    Thanks Eric. Works as expected.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.