MySQL String Object to PHP array conversion - php

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.

Related

How to convert JSON into an array or string?

I'm not sure if I'm allowed to ask these types of questions. Anyway, I have this array value $custom_fields['user_gender'][0] that outputs this:
a:2:{i:0;s:7:"Male";i:1;s:7:"Female";}
I'm not sure how to convert this into an array or string.
The example data looks like it is serialized PHP. Serializing is a way an array, string etc. can be "represented" as a textual string and can later be converted back to the original data format, using a unserialize operation. A serialized string can be stored as text in a database and can be unserialized at any point.
You can unserialize it with:
$array = unserialize($custom_fields['user_gender'][0]);
This should return an array. If you var_dump on $array, you can be sure it's an array.

Parsing twitter stdClass string in 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

Convert TLV to PHP array

I have a Type Length Value string:
a:4:{s:5:"value";s:4:"0.00";s:4:"type";N;s:12:"discounttype";s:10:"Percentage";s:13:"configoptions";N;}
Is it possible to convert it to PHP array or JSON string?
That kind of string could be done with unserialize() function. Also there is an article about that solving your needs here.
After it has been unserialized, it is an array. You can then convert using json_encode() function.
If you need extra help, write a comment

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

Data stored in json

I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!
That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.
#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';
#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.
This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.

Categories