Search code examples
arraysparsingnotation

How can I parse this unknown array notation? What language is this array notation from?


I am accessing data from an old database and several of the records have this array notation which defines the criteria for a real estate search. There are several hundred and I need to convert them to data I can use in JS and PHP.

Here is an example of the array notation. I haven't been able to find any other questions asking about this format.

a:14:{s:2:"id";s:22:"our-listings-metrolist";s:3:"map";a:4:{s:8:"latitude";s:17:"38.93309311783631";s:9:"longitude";s:19:"-120.74187943878752";s:4:"zoom";s:1:"8";s:4:"open";s:1:"0";}s:4:"feed";s:15:"ncarmetrolistca";s:6:"panels";a:2:{s:9:"office_id";a:3:{s:7:"display";s:1:"1";s:9:"collapsed";s:1:"0";s:6:"hidden";s:1:"0";}s:4:"type";a:3:{s:7:"display";s:1:"1";s:9:"collapsed";s:1:"0";s:6:"hidden";s:1:"0";}}s:9:"office_id";s:5:"01PHA";s:11:"search_type";s:0:"";s:3:"idx";s:15:"ncarmetrolistca";s:14:"search_subtype";s:0:"";s:10:"snippet_id";s:22:"our-listings-metrolist";s:13:"snippet_title";s:42:"Our Sacramento / Sierra Foothills Listings";s:10:"page_limit";s:1:"6";s:7:"sort_by";s:17:"DESC-ListingPrice";s:4:"view";s:4:"grid";s:12:"price_ranges";s:4:"true";}

It's not hard to understand and I will write my own parser if I have to but I'm hoping I don't need to. a defines an array, s defines a string, and i defines an integer. The integer after each definition character defines the length of the array, string, or integer, and then the value defined at that position representing either a key or a value.

What kind of notation is this? Is there someway I can parse this quickly into a format that can be used in JS and PHP. Do I need to build my own parser?


Solution

  • That's the serialization of an object in php.

    For instance:

     $obj = ['a'=>1, 'b'=>true, 'c'=>'foo'];
     echo serialize($obj); /* prints: a:3:{s:1:"a";i:1;s:1:"b";b:1;s:1:"c";s:3:"foo";} */
    

    To unserialize, just use the unserialize() function.