0

I have a JSON string which is stored in a MYSQL DB. Example:

stdClass Object (
[product] => stdClass Object
    (
        [sold_individually] => 
        [regular_price] => 
        [managing_stock] => 1
        [sku] => 0001
        [title] => Sample title
        [reviews_allowed] => 1...

etc.

I have used utf8_encode() to convert it. How can I then turn this in to a PHP array so I can use it?

Edit 1

I have now used json_encode() when posting to the DB. I have also had to use addSlashes() as I was getting some formatting issues with some of the values.

When I now pull in the data it looks like:

"{\"product\": {\"sold_individually\": false, \"regular_price\": \"\", \"managing_stock\": true, \"sku\": \"W-C-6500\", \"title\": \"Sample title\"...

How can I pull out the value for "title"? Should I also use str_replace() to get rid of the \ issue?

Ian Fraser
  • 133
  • 2
  • 13

3 Answers3

1

What you printed is not JSON, it's the string representation of a php Object I think. So if possible, you should store into your database actual JSON. It's easier, cleaner and more compact.

To do so, use json_encode($your_object) to insert the data in your database.

To retrieve it, use json_decode($string_from_database).

This should work like a charm.

Note: There's a trap though, by default, json_decode will create a php Object and not an array. So you have to retrieve your fields as attributes of an object:

$data = json_decode($string_from_database);
$data->product->title; // to access title in your data structure

But if you want an array, you can provide an argument to get an array and then it works like this:

$data = json_decode($string_from_database, true);
$data['product']['title']; // to access title in your data structure

Check the php documentation of json_decode for further info!

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

Louis Durand
  • 187
  • 9
  • Thank you this has really helped. I realised I has set json_decode before posting it which was the issue. I am now able to use json_encode when posting it and json_decode to retrieve it. How do I get access to a particular field though? ie: title – Ian Fraser Oct 12 '16 at 02:12
  • Oh yeah, there's a trap, by default, json_decode will create a php Object and not an array. So you have to retrieve your fields as attributes of an object: $data = json_decode($string_from_database); $data->product->title; // to access title in your data structure – Louis Durand Oct 12 '16 at 16:20
  • But if you want an array, you can provide an argument to get an array like this: json_decode($string_from_database, true) and in that case, you can access the data like this: $data['product']['title'] Ceck the php documentation of json_decode for further info! – Louis Durand Oct 12 '16 at 16:25
  • Thanks Louis. I realised yesterday we had posted it as a string and not pure JSON. I managed to use what you have posted above and it now works like a treat ;) thank you. – Ian Fraser Oct 13 '16 at 22:08
0

There is a utility called utf8_decode(string) which converts previously encoded with the utf8_encode() function, back to ISO-8859-1. From there you have utility available to make a string a array/Json,etc.

Community
  • 1
  • 1
Cyclotron3x3
  • 2,188
  • 23
  • 40
0

Problem solved. I was posting the JSON as a string and not pure JSON which prevented me from using json_decode when it came back out.

Ian Fraser
  • 133
  • 2
  • 13