forumnz Posted May 8, 2009 Share Posted May 8, 2009 Hey I have a JS script that POSTs this: [{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"},{"field2":"cgh","field4":73377,"field3":770}] to a PHP file. What can I do to insert that information into a database? Note that the data between {} is it's own row. Basically, how can I get that into some sort of suitable array? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/ Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Again! json_decode. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829209 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks, but as you can see there are numbers not enclosed in double quotes. So that work work? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829210 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Did you at least try? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829212 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Yes it returns 1. It says that if the string is invalid, I returns 1 or NULL. I'm trying to add double quotes, but not doing well. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829213 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 I thought you wanted to store them in the DB. Look - $e = '{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"}'; var_dump(json_decode($e, true)); You don't like the output of that? It's an array you should be familiar with. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829215 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 This is what I have for the quotes problem: <?php $str = $data; $str = substr($str, 1, strlen($str) - 2); $str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str); // fix left side $str = preg_replace("/:(^false^true[a-zA-Z_]+[a-zA-Z0-9_]*)/", ":\"$1\"", $str); // fix right side $data = json_decode($str, true); $data = print_r($data); ?> Still returns 1 though..? Why would that be? I do want to store them in a database but I need to get the data into an array for that don't I? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829217 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Read edited post. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829218 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks Ken2k7 I modified one of your lines to read: <?php $e = '[{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"},{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"}]'; ?> Which is basically 2 sets of data. And the output is: array(2) { [0]=> array( { ["field3"]=> int(66) ["field4"]=> int(5) ["field1"]=> string(3) "ggg" ["field2"]=> string(1) "g" ["field5"]=> string(1) "h" ["field6"]=> string(1) "h" ["field7"]=> string(2) "hh" ["id"]=> string(1) "1" } [1]=> array( { ["field3"]=> int(66) ["field4"]=> int(5) ["field1"]=> string(3) "ggg" ["field2"]=> string(1) "g" ["field5"]=> string(1) "h" ["field6"]=> string(1) "h" ["field7"]=> string(2) "hh" ["id"]=> string(1) "1" } } Which is fine (I believe). What would I need to do now? Much appreciated Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829224 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Run a SQL to put them in the DB? Come on dude, let's not take baby steps here. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829230 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 I'm really trying It's just so difficult for me. Would I do some sort of loop now? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829233 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 For the most part, arrays == looping. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829234 Share on other sites More sharing options...
forumnz Posted May 8, 2009 Author Share Posted May 8, 2009 Ok so the output now is basically the VALUES and I really just need to put that variable into the database query? Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829235 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Just test it out. I don't know how you want to store stuff in your DB. If something went wrong or you got errors, we can help you fix those. Link to comment https://forums.phpfreaks.com/topic/157324-what-can-i-do-with-this-string/#findComment-829389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.