PHP: Converting this data structure into an associative array - php

I have a field created by word press which contains data with the following structure.
a:16:{s:7:"country"
s:14:"United Kingdom"
s:7:"form_id"
s:2:"35"
s:9:"timestamp"
s:10:"1560869327"
s:7:"request"
s:0:""
s:8:"_wpnonce"
s:10:"125"
s:16:"_wp_http_referer"
s:1:"/"
s:17:"ajaxy-umajax-mode"
s:8:"register"
s:10:"first_name"
s:5:"xxxxx"
s:9:"last_name"
s:5:"xxx"
s:10:"user_email"
s:28:"xxx#xxx.co.uk"
s:7:"Company"
s:16:"xxx LTD"
s:12:"phone_number"
s:10:"0123456789"
s:8:"user_url"
s:20:"http://www.test.com"
s:15:"company_address"
s:18:"999 LockSmith Lane"
s:12:"display_name"
s:12:"XXXX"
s:10:"user_login"
s:10:"xxx123"
}
I want to convert this to an array so I can read the properties of it.
I tried converting it to json but its not json.
Any ideas on how I can parse this data, or access its properties in PHP.
I can not access this data through wordpress as my PHP script is part of something else.

Seems like a serialized array. Try unserializing it to convert it back to normal, see example.

This is Actually not a json.This is an array in serialized format you can just unserilize it by using this function
maybe_unserialize($YOUR_ARRAY).
maybe_unserialize is a wordpress default function to unserialize the array

Related

What kind of data is this, and is it able to change to JSON

I'm working on cargo wordpress plugin, the shipping history page stores the following data in meta_value field in database. Does anyone know what kind of data is this, and how should I use it or change to a JSON.
s:815:"a:4:{i:0;a:7:{s:4:"date";s:10:"2018-03-08";s:4:"time";s:7:"1:00 am";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:16:"Shipment Left US";}i:1;a:7:{s:4:"date";s:10:"2018-03-08";s:4:"time";s:7:"1:00 am";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"Processing";}i:2;a:7:{s:4:"date";s:10:"2018-09-12";s:4:"time";s:7:"7:27 pm";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"In Transit";}i:3;a:7:{s:4:"date";s:10:"2018-09-12";s:4:"time";s:7:"7:31 pm";s:8:"location";s:10:"tofindthis";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"In Transit";}}";
That looks like a serialized array.
To convert it to JSON you need to deserialize it first and then json_encode it
How to use php serialize() and unserialize()
echo json_encode($arr);
will echo the json enconded string where $arr is the deserialized array.

JSON decode is not working

This is my 3.txt file:
I don't know why it works when it has only 1 data and when it has more data it's not working.
This is my code:
Your 3.txt does not contain a valid json. You need to adjust your sample string to be valid JSON by adding quotes around strings, commas between objects and placing the objects inside a containing array (or object).
It should be something like this,
[{"address":"+6161616","body":"asda","date":"1231"},
{"address":"+616sdfs6","body":"as3sda","date":"155231"},
{"address":"+6161616","body":"asda","date":"123551"}]
You can test if a json is valid here.

How do I decode data in mysql field that is in this format?

I have fields in my database that I need to decode or view in a more simple format. I'm not sure what method was used to create this format though I've seen it before. This is data from a web form in a MySQL table. What do I use in PHP or MySQL to decode this when I retrieve it from the database?
a:10:{s:10:"First Name";s:10:"cvsgjsrhlw";s:9:"Last Name";s:10:"cvsgjsrhlw";s:7:"Address";s:26:"http://www.tlneepxlni.com/";s:4:"City";s:7:"Atlanta";s:5:"State";s:2:"AL";s:8:"Zip Code";s:0:"";s:9:"Best time";s:7:"Mid-day";s:6:"Other2";s:8:"cqoeqipd";s:14:"Procedure face";s:18:"Laser Hair Removal";s:4:"when";s:22:"In the next 4 months.";}
It's a built in php serialization
You need to use unserialize
This is serialize()'d code. It is a way to multiple data types as plain text. You can convert it back to a php object with unserialize($data)
If you are writing new code, I would recommend using json_encode() instead
This is a serialized array. Use:
$data_array = unserialize($data_from_db);

How to Extract Data from a field with multiple values?

IP.Nexus puts custom fields into a table all muddled up into one row and some how pulls the correct data out.
basically my custom fields row looks like so:
a:2:{i:2;s:4:"Test";i:3;s:10:"Accounting";}
How do i select what item to pull as i would like to out put just one item not the whole row, is it with delimiters or string splitter, as i am novice with this part of mySQL
Cause i would like to echo out just the advertising on its own and also vice versa for the testing.
Thanks in advance
It's a serialized array using serialize() function so you can get its contents using unserialize($YourString);.
But I see that you string is corrupted for deserialization, because s:4: in s:4:"advertising" says that after s:4: 4 character long string is expected that advertising is not.
The data in the database is in serialized(json) form.You can turn it into simple array using unserialize.
For example;if $mystr is your string
write
$mystring=unserialize($mystring)
You will get normal array of this string
You can do
foreach($mystring as $str){
$ads[]=$str['advertising'];
}
and the resulting array $ads will be the ads you like in simple array form
Since this looks as JSon, you can use a PHP JSon module to parse each row from MySQL.
<?php
(...)
while (...) {
$obj = json_decode($row);
// do something with $obj
}

dump xml string verbatim to mysql db

I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.

Categories