alecthorne Posted November 7, 2015 Share Posted November 7, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/299342-help-with-json_decode/ Share on other sites More sharing options...
requinix Posted November 7, 2015 Share Posted November 7, 2015 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"; Quote Link to comment https://forums.phpfreaks.com/topic/299342-help-with-json_decode/#findComment-1525909 Share on other sites More sharing options...
benanamen Posted November 7, 2015 Share Posted November 7, 2015 Third, you are using deprecated MySql Code. You need to use PDO with prepared statements. Quote Link to comment https://forums.phpfreaks.com/topic/299342-help-with-json_decode/#findComment-1525915 Share on other sites More sharing options...
alecthorne Posted February 14, 2016 Author Share Posted February 14, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/299342-help-with-json_decode/#findComment-1531104 Share on other sites More sharing options...
requinix Posted February 15, 2016 Share Posted February 15, 2016 $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. Quote Link to comment https://forums.phpfreaks.com/topic/299342-help-with-json_decode/#findComment-1531118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.