Raju  Dudhrejiya
Raju Dudhrejiya

Reputation: 1727

How to convert json data to array in php?

I have json string live below, I try to convert array but I am not success can anyone please help me, thank you advance.

Example 1 : s:59:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"}]";

Example 1 : s:109:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"},{"item_id":"PESA VALIGIA","qty":1,"points":"120"}]";

Upvotes: 0

Views: 6603

Answers (5)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

Your data is serialized as well as json encoded. So you have to use:-

json_decodealong with unserialize like below:-

<?php

$data = 's:59:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"}]"';
print_r(json_decode(unserialize($data),true));
?>

And

<?php

$data = 's:109:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"},{"item_id":"PESA VALIGIA","qty":1,"points":"120"}]"';
print_r(json_decode(unserialize($data),true));
?>

https://eval.in/590757 And https://eval.in/590758

For more reference:-

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.unserialize.php

Upvotes: 0

Ravi Hirani
Ravi Hirani

Reputation: 6539

Use unserialize and json_decode

json_decode(unserialize($string),true); // pass second argument true

When true, returned objects will be converted into associative arrays.

Upvotes: 2

Atul Cws
Atul Cws

Reputation: 7

seems like these are serialized string not json encoded..

use json_decode(unserialize($string)); to get array.

Upvotes: -1

Gino Pane
Gino Pane

Reputation: 5001

It seems, that you have a serialized json, so try this:

$array = json_decode(unserialize($string), true);

But it also seems that your data is corrupted, that's why unserialize does not work correctly in some PHP versions. If this is your case then in this question you can find a way to fix this: unserialize() [function.unserialize]: Error at offset.

Upvotes: 3

Alok Patel
Alok Patel

Reputation: 8012

Read http://php.net/manual/en/function.json-decode.php

$array=json_decode($JSON_STRING,true);

Second parameter in function is for getting result as an Array, by default it returns Object.

Upvotes: -1

Related Questions