Jump to content

Help with json_decode


alecthorne

Recommended Posts

I am using the application ZOO with Joomla, which stores information within a multi dimensional array with a field named 'elements'. The format is similar to that shown below.

 

 {
    "0ad767a2-b0d6-42fe-abb9-cf7d3e62c88b":  {
        "0":  {
            "value": "NameABCD"
        }
    },
    "4adc7e1d-1124-450a-986f-a4df1bb7b4c0":  {
        "0":  {
            "value": "E-MailABCD"
        }
    },
}

 

I am not a php programmer, but with help I managed to extract the information using the a @mysql_query and the following:

 

        $elements = $row['elements'];
        $zooelements = json_decode($elements);
        $name = $zooelements->{"0ad767a2-b0d6-42fe-abb9-cf7d3e62c88b"}->{"0"}->value;

 

My problem is if I want to change any value e.g. $name="NameEFGH", how can I propogate this new value back to the array using a @mysql_query such as:

 

                $query = "UPDATE link_zoo_item SET elements=????

 

Any help as a beginner would be much appreciated,

 

Alec

Link to comment
Share on other sites

First, use an array for the decoded JSON. Having to use that {""} syntax is unfortunate and awkward.

$zooelements = json_decode($elements, true);
$name = $zooelements["0ad767cantbebotheredtocopypaste"][0]["value"];
Second, are you actually using a @ when you call mysql_query? That will hide any errors that may happen. Hiding errors is very bad. Don't use @.

 

Is your question about how to update $zooelements? Surely the answer is just

$zooelements["0adblahblahblah"][0]["value"] = "NameEFGH";
Link to comment
Share on other sites

  • 3 months later...

Dear requinix and benanamen, apologies for the extremely late reply to both your answers.

 

Sadly the syntax of the array is set by the ZOO application I use for Joolma.

 

The PHP routines I wrote originally are extremely old, and sadly represent my lacking knowledge in PHP, but I would like to update the values in-line with what I have written before (if possible)

 

I tried the following, but the new value is not returned to the array

 

$zooelements["de37ea08-9c88-4479-8453-fc020fe91eb9"] = "NameEFGH";

$query5 = "UPDATE link_zoo_item SET elements=$zooelements WHERE id=$id";
$result5 = mysql_query($query5);

 

Is there something that I have missed?

 

Many thanks,

 

Alec

Link to comment
Share on other sites

$zooelements is an array. You can't just stick an array into a string like that.

 

Do you mean to store the JSON? Use json_encode() to turn the modified array back into a JSON string, then escape it (with mysql_real_escape_string()) before sticking it, quoted, into the SQL.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.