-1

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....

Hardik Patel
  • 67
  • 1
  • 16

4 Answers4

2

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

sshet
  • 1,152
  • 1
  • 6
  • 15
  • Thanks but, I have tried bro... Look this... $locations='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";}}'; $resultLocations=json_decode($locations,TRUE); print_r($resultLocations); – Hardik Patel Apr 16 '14 at 06:14
  • right , so first you need to correct JSON string otherwise it will keep on throwing error – sshet Apr 16 '14 at 06:26
1

Here is the Best way:

  1. Store the string in a PHP variable:

    $jsondata = '...... json string goes here ......';

  2. 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

Sankalp Bhatt
  • 1,154
  • 13
  • 17
1

try json_decode()

refer php manual

AVM
  • 592
  • 2
  • 11
  • 25
0

Got solution to your problem:

  1. 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.

  2. 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.

Sankalp Bhatt
  • 1,154
  • 13
  • 17