Support » Plugins and Hacks » [Resolved] Store an array, and get back an object?

[Resolved] Store an array, and get back an object?

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

        [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


Viewing 4 replies - 1 through 4 (of 4 total)
  • 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.

    Plugin Author Eric Mann


    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.

    Thanks, I’ll give that a go.



    Thanks Eric. Works as expected.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Store an array, and get back an object?’ is closed to new replies.