Parsing twitter stdClass string in PHP - php

I have this string stored in a database column:
O:8:"stdClass":2:{s:4:"type";s:5:"Point";s:11:"coordinates";a:2:{i:0;d:-23.5663719;i:1;d:-46.65284157;}}
And I just want the "coordinates" in the middle:
-23.5663719,-46.65284157
There is a easy way to parse it in PHP to get this value?
I tried json_decode but it didn't work.
Thanks.

It's not json encoded, but serialized using serialize().
So what you have to do is:
$o = unserialize($str); // where $str is a string fetched from DB
var_dump($o->coordinates);
References:
http://www.php.net/manual/en/function.serialize.php
http://www.php.net/manual/en/function.unserialize.php

Related

Serilaizing in PHP returns wrong value

I try to serialize my data with PHP. Unfortunaly, the serialize() function returns a wrong value.
String to be serialized:
{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}
What serialize() returns on my server:
serialize($code);
s:204:"{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}";
What should be returned (https://duzun.me/playground/serialize):
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
You need to json_decode it first to get the wanted result:
When you use the boolean switch as second parameter in json_decode it will be an array instead of an object.
$serialized = serialize(json_decode($inputString, true));
echo $serialized;
// output:
// a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
The site you're using isn't clear about what it's doing, but it appears to be treating the string as JSON and decoding to an array before serializing it as PHP. If you want to replicate this, you can use:
$str = '{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}';
echo serialize(json_decode($str, true));
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
As pointed out in the comments, unless there's a specific reason you need serialized PHP, then just stick with the serialized JSON string you already have - it'll be both more readable and portable.

PHP and JSON datatype in MySQL

Despite of recent implementation of JSON datatype to MySQL I can't find any word on it in the related PHP documentation.
My question is: will PHP automatically convert cells of JSON column to the actual values - arrays or literals - or will it provide just json-encoded strings. Like:
$sql_query = "SELECT JSON_ARRAY(1,2,3)";
$result = mysqli_query($sql_query);
$value = mysqli_fetch_row($result)[0];
// what is a $value? Array(1,2,3) or a string "[1,2,3]"
// do I have to use json_decode() to get an actual array here?
(Don't have MySQL 5.7 at hand right now, so can't check it myself.)
i think with serialize and unserialize you get whatever you want.
so just store all values using serialize and you can get that values with unserialize.
so just check serialize and unserialize.

MySQL String Object to PHP array conversion

I have this object string in my MySQL database and I need to convert it to a PHP array. I believe it's a multi-level Object, but I'm unsure on how to parse it into a usable array.
a:11:{s:7:"version";s:3:"4.3";s:9:"increment";i:2;s:7:"convert";b:0;s:7:"dynamic";b:0;s:8:"gestures";b:0;s:9:"integrate";b:0;s:9:"shortcuts";b:0;s:9:"uninstall";b:0;s:5:"sizes";a:0:{}s:5:"terms";a:0:{}s:11:"collections";a:1:{s:9:"webcomic1";a:15:{s:2:"id";s:9:"webcomic1";s:4:"name";s:13:"Feedback Inc.";s:5:"image";s:1:"0";s:5:"theme";s:29:"twentyfourteen|twentyfourteen";s:7:"updated";i:1427408379;s:8:"supports";a:9:{i:0;s:5:"title";i:1;s:7:"excerpt";i:2;s:6:"editor";i:3;s:9:"thumbnail";i:4;s:8:"comments";i:5;s:10:"trackbacks";i:6;s:9:"revisions";i:7;s:13:"custom-fields";i:8;s:6:"author";}s:10:"taxonomies";a:2:{i:0;s:8:"category";i:1;s:8:"post_tag";}s:11:"description";s:0:"";s:5:"feeds";a:3:{s:4:"hook";b:1;s:4:"size";s:4:"full";s:4:"main";b:1;}s:5:"slugs";a:5:{s:4:"name";s:8:"feedback";s:7:"archive";s:8:"feedback";s:8:"webcomic";s:8:"feedback";s:9:"storyline";s:8:"feedback";s:9:"character";s:8:"feedback";}s:6:"buffer";a:3:{s:4:"hook";b:1;s:4:"days";i:7;s:5:"email";s:25:"mlarson#primary-intel.com";}s:6:"access";a:4:{s:5:"byage";b:0;s:6:"byrole";b:0;s:3:"age";i:18;s:5:"roles";a:1:{i:0;s:1:"!";}}s:7:"twitter";a:8:{s:5:"media";b:0;s:6:"format";s:39:"%collection-name has updated! %site-url";s:11:"oauth_token";s:0:"";s:12:"oauth_secret";s:0:"";s:12:"consumer_key";s:0:"";s:15:"consumer_secret";s:0:"";s:13:"request_token";s:0:"";s:14:"request_secret";s:0:"";}s:8:"commerce";a:9:{s:8:"business";s:0:"";s:8:"currency";s:3:"USD";s:6:"method";s:7:"_xclick";s:8:"donation";i:0;s:5:"price";a:3:{s:8:"domestic";i:6;s:13:"international";i:9;s:8:"original";i:12;}s:8:"shipping";a:3:{s:8:"domestic";i:4;s:13:"international";i:6;s:8:"original";i:8;}s:6:"prints";b:0;s:9:"originals";b:0;s:5:"total";a:3:{s:8:"domestic";d:10;s:13:"international";d:15;s:8:"original";d:20;}}s:11:"transcripts";a:4:{s:4:"open";b:1;s:9:"languages";a:1:{i:0;s:1:"!";}s:10:"permission";s:8:"register";s:6:"notify";a:2:{s:4:"hook";b:1;s:5:"email";s:25:"mlarson#test.com";}}}}}
Could someone:
itentify what type of string this is?
Explain how to parse it into a usable PHP array?
That's the format that serialize() produces. Use unserialize to parse it into an array.
$array = unserialize($string);
It's irrelevant that it came from MySQL.

Parse a json(?) string using php

I have a string, more specifically, this one:
a:16:{s:9:"pseudonym";O:16:"SimpleXMLElement":0:{}s:14:"parallel_title";O:16:"SimpleXMLElement":0:{}s:9:"title_var";O:16:"SimpleXMLElement":0:{}s:6:"series";O:16:"SimpleXMLElement":0:{}s:9:"vol_title";O:16:"SimpleXMLElement":0:{}s:9:"reference";O:16:"SimpleXMLElement":0:{}s:10:"bound_with";O:16:"SimpleXMLElement":0:{}s:15:"general_remarks";O:16:"SimpleXMLElement":0:{}s:6:"copies";O:16:"SimpleXMLElement":1:{i:0;s:1:"1";}s:11:"remarks_BPH";O:16:"SimpleXMLElement":0:{}s:3:"ICN";O:16:"SimpleXMLElement":1:{i:0;s:4:"neen";}s:10:"provenance";O:16:"SimpleXMLElement":0:{}s:7:"binding";O:16:"SimpleXMLElement":0:{}s:10:"size_hxwxd";O:16:"SimpleXMLElement":0:{}s:6:"BookID";O:16:"SimpleXMLElement":1:{i:0;s:4:"6271";}s:5:"repro";O:16:"SimpleXMLElement":0:{}}
Is it possible to parse this string somehow? I need to display the keys and values in a list. I tried to use json_decode but it doesn't return anything, even with the second parameter set to true:
json_decode($string,true);
It's not JSON, it's serialized PHP. Use unserialize().
It's serialize object
Read more on PHP website

how to convert json to string

how do i convert a json object to a string
i want to insert a json object to a mysql DB
You might be interested in json_encode().
On the other hand if you already got something json encoded then it already is a string and you can simply store it as-is in the database.
JSON itself is a string.
People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays.
$array = json_decode($json,true);
$string = implode(",",$array);
Now you can save this string in db as text.

Categories