acidpunk Posted July 22, 2014 Share Posted July 22, 2014 Hey everyone. So i'm trying to json_encode one of my arrays like such: heres an example of how the array is being created (From the array results, everything seems to be fitting OK into the actual array creation) but it's just the actual array creating a multi demension after each result. $num = 1; $num2 = 1; $i = 0; if ($num > 0) { $array[$i][1] = 40; } if ($num2 > 0) { $array[$i][2] = 50; } ++$i; I'm trying to achive this: var array_one_results = new Array(); array_one[1][0] = 40; array_one[1][0] = 50; array_one[2][1] = 40; array_one[2][1] = 50; right now, this is my results: var array_name = new Array(); array_name["0"] = new Array();array_taken["0"]["0"] = "10"; array_taken["0"]["1"] = "20"; array_name["1"] = new Array();array_taken["1"]["0"] = "10"; array_taken["1"]["1"] = "20"; array_name["2"] = new Array();array_taken["2"]["0"] = "10"; array_taken["2"]["1"] = "20"; array_name["3"] = new Array();array_taken["3"]["0"] = "10"; array_taken["3"]["1"] = "20"; array_name["4"] = new Array();array_taken["4"]["0"] = "10"; array_taken["4"]["1"] = "20"; It's creating a new array for each. If anyone could help me through this, i'd really appreciate it. I'm encoding it with this: function js_str($s) { return '"'.addcslashes($s, "\0..\37\"\\").'"'; } function js_array($array, $keys_array) { foreach ($array as $key => $value) { $new_keys_array = $keys_array; $new_keys_array[] = $key; if(is_array($value)) { echo 'array_name'; foreach($new_keys_array as $key) { echo '["'.$key.'"]'; } echo ' = new Array();'; js_array($value, $new_keys_array); } else { echo 'array_taken'; foreach($new_keys_array as $key) { echo '["'.$key.'"]'; } echo ' = '.js_str3($value).";\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/290067-help-with-json_encode/ Share on other sites More sharing options...
requinix Posted July 22, 2014 Share Posted July 22, 2014 (edited) I'm trying to achive this: var array_one_results = new Array(); array_one[1][0] = 40; array_one[1][0] = 50; array_one[2][1] = 40; array_one[2][1] = 50; I assume you mean var array_one_results = new Array(); array_one[1][0] = 40; array_one[1][1] = 50; array_one[2][0] = 40; array_one[2][1] = 50; As long as you can get your PHP array to look exactly like the Javascript "array" you want, var array_name = <?=json_encode($array)?>;That's all it takes. Edited July 22, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/290067-help-with-json_encode/#findComment-1485952 Share on other sites More sharing options...
acidpunk Posted July 22, 2014 Author Share Posted July 22, 2014 Yeah, sorry I mistyped it. But thank you for the response, I actually originally did do exactly what you said, but it did not work. Quote Link to comment https://forums.phpfreaks.com/topic/290067-help-with-json_encode/#findComment-1485953 Share on other sites More sharing options...
Jacques1 Posted July 23, 2014 Share Posted July 23, 2014 What is this weird JavaScript code generator stuff, anyway? I mean, what are you trying to achieve in the end? Generating code is generally a very bad idea, and it's particularly bad in a web context. You waste your time struggling with syntax noise, you'll probably screw up some detail in the highly complex JavaScript grammar, and you invite the whole world to try a code injection attack. For example, if you plan to put the generated code into a script element, you've already lost, because you've failed to escape critical characters like the backslash or the ampersand which can be used for a cross-site scripting attack. Long story short: You're almost certainly doing it wrong. If you want to exchange data, you should do exactly that. Just make a script which JSON-encodes the array with the built-in json_encode() function and echos the result. You can then fetch the data with Ajax. Quote Link to comment https://forums.phpfreaks.com/topic/290067-help-with-json_encode/#findComment-1485955 Share on other sites More sharing options...
requinix Posted July 23, 2014 Share Posted July 23, 2014 But thank you for the response, I actually originally did do exactly what you said, but it did not work.If you say that because you got object notation {...} then that doesn't matter much: objects and arrays in Javascript are very nearly interchangeable. (That's why I said "array" with quotation marks.) How does it "not work"? Or put another way, what output are you getting and what are you trying to get? Because like Jacques1 said json_encode() is what you should be using, so any problems you're having will about how you're constructing the PHP array or using the Javascript array. Quote Link to comment https://forums.phpfreaks.com/topic/290067-help-with-json_encode/#findComment-1485960 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.