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 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 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 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 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 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 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 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 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.