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?

3
  • have you tried PHP's "json_decode" function? Commented Oct 12, 2016 at 1:23
  • did you try $array = json_decode($json,1); ? Commented Oct 12, 2016 at 1:27
  • Yes, I have tried using json_decode() and I get NULL back. Do I need to turn the string in to an object? Commented Oct 12, 2016 at 1:32

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.