Skip to content

unserialize with assoc=true disobeys docs #21

@jqr

Description

@jqr

The docs suggest PHP.unserialize with assoc = true should return an array of key value pair arrays when a zero indexed, incrementing, associative array is encountered.

# If a PHP array (associative; like an ordered hash) is encountered, it
# scans the keys; if they're all incrementing integers counting from 0,
# it's unserialized as an Array, otherwise it's unserialized as a Hash.
# Note: this will lose ordering.  To avoid this, specify assoc=true,
# and it will be unserialized as an associative array: [[key,value],...]

But this doesn't seem to hold in practice:

#                                                   +-index-+
#                                                   |       |
#                                                  ---     ---
serialized = PHP.serialize([5, 6])      # => "a:2:{i:0;i:5;i:1;i:6;}"

PHP.unserialize(serialized, {}, false)  # => [5, 6]                    # as expected
PHP.unserialize(serialized, {}, true )  # => [5, 6]                    # expected [[0, 5], [1, 6]]

Compare with a non zero indexed incremeting associative array:

other = PHP.serialize("x" => 7, "y" => 8)   # => 'a:2:{s:1:"x";i:7;s:1:"y";i:8;}'

PHP.unserialize(other, {}, false)           # => { "x" => 7, "y" => 8 }
PHP.unserialize(other, {}, true )           # => [["x", 7], ["y", 8]]

So it looks like the assoc argument to unserialize currently converts associative arrays to nested arrays UNLESS they are zero indexed incrementing arrays, in which case it does nothing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions