Pain Posted August 19, 2014 Share Posted August 19, 2014 (edited) Hello. I was wondering how can I add a php array to a js array containing json values. var availableTags = [ "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; I want to add this to the js array: <?php $arr = array('red', 'green', 'blue'); ?> I've tried doing this, but its definitely the wrong approach as it just sums everything up into one element "redgreenblue" var availableTags = [ "<?php foreach($arr as $row) { echo $row; } ?>", "AppleScript", "Asp", "BASIC", .... Thanks for any kind of help! Edited August 19, 2014 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/ Share on other sites More sharing options...
CroNiX Posted August 19, 2014 Share Posted August 19, 2014 (edited) Where is the js array stored? Somehow you'd have to get that into php-land, or vice-versa. Then just json_decode(js_array) and array_merge() it with your php array, or the other way around (json_encode(php_array) and concatenate the 2 js arrays using jsarray.concat(other_array)) Edited August 19, 2014 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488356 Share on other sites More sharing options...
Pain Posted August 19, 2014 Author Share Posted August 19, 2014 It's just this one variable - var availableTags = []. it is used in a script later on. Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488358 Share on other sites More sharing options...
CroNiX Posted August 19, 2014 Share Posted August 19, 2014 Yes, but that's a javascript array. Is it being generated by php or something before you stick it on the page? Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488360 Share on other sites More sharing options...
Pain Posted August 19, 2014 Author Share Posted August 19, 2014 Not really, it is pre-populated with values Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488361 Share on other sites More sharing options...
CroNiX Posted August 19, 2014 Share Posted August 19, 2014 I'm not sure how you would do that then. One is in the browser (js) and the other is on the server (php). Somehow they need to be in the same place in order to merge them. Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488363 Share on other sites More sharing options...
Pain Posted August 19, 2014 Author Share Posted August 19, 2014 Thanks for your effort. I have solved it by doing this: <?php $php_cities_array = array('smth', 'smth2'); function js_str($s) { return '"' . addcslashes($s, "\0..\37\"\\") . '"'; } function js_array($array) { $temp = array_map('js_str', $array); return '[' . implode(',', $temp) . ']'; } $array = 'var availableTags = ' . js_array($php_cities_array) . ';'; ?> <script> <?php echo $array; ?> </script> Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488365 Share on other sites More sharing options...
CroNiX Posted August 19, 2014 Share Posted August 19, 2014 Something like this might work for you. var availableTags = [ "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; <?php $arr = array('red', 'green', 'blue'); echo "var php_array = " . json_encode($arr) . ';'; ?> availableTags = availableTags.concat(php_array); Quote Link to comment https://forums.phpfreaks.com/topic/290543-add-php-array-to-json-array/#findComment-1488367 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.