I want to convert the following JSON string to array using PHP...
a:6:{
i:0;a:2:{s:2:"id";d:31003200320082;s:7:"address";s:5:"test1";}
i:1;a:2:{s:2:"id";d:83003500350087;s:7:"address";s:9:"test2";}
i:2;a:2:{s:2:"id";d:41002800280012;s:7:"address";s:5:"test3";}
}
Please help me....
Try using
json_decode($data,true)
to get the results in Array format
true is for converting to array, otherwise it will return as object
Here is the Best way:
Store the string in a PHP variable:
$jsondata = '...... json string goes here ......';
Now use json_decode PHP function.
$resultdata = json_decode($jsondata);
The result will be collection of arrays and objects.
However, your data is not is json but serialized so you will need to use unserialize
try json_decode()
refer php manual
Got solution to your problem:
The String you are using is not a JSON string, its actually a serialized array. So you will need to use the function unserialize instead of json_decode.
You are not getting success with unserialize because the serialized string you have is malformed. I have made some adjustments in the string use mine one and you will get success.
old malformed one:
a:6:{
i:0;a:2:{s:2:"id";d:31003200320082;s:7:"address";s:5:"test1";}
i:1;a:2:{s:2:"id";d:83003500350087;s:7:"address";s:9:"test2";}
i:2;a:2:{s:2:"id";d:41002800280012;s:7:"address";s:5:"test3";}
}
new Corrected string:
a:3:{i:0;a:2:{s:2:"id";i:31003200320082;s:7:"address";s:5:"test1";}i:1;a:2:{s:2:"id";i:83003500350087;s:7:"address";s:5:"test2";}i:2;a:2:{s:2:"id";i:41002800280012;s:7:"address";s:5:"test3";}}
Have a good day my friend.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.