mjcnyx Posted January 11, 2012 Share Posted January 11, 2012 I have a simple PHP array such as: $arr = array("red car", "blue truck", "black jeep", "yellow bike"); I would like to convert this array to a JQuery array, but have no idea where to begin. Thanks for any advice in advance. Link to comment Share on other sites More sharing options...
Ingolme Posted January 11, 2012 Share Posted January 11, 2012 You mean a Javascript array? jQuery is just a Javascript library.Just print out all the elements in a Javascript syntax. echo 'var arr = [';$comma = false;foreach($arr as $item) { // Make sure we don't add a comma before the first item of the JSON syntax array. if($comma) { echo ','; $comma = true; } echo " '{$item}' ";}echo ']'; Link to comment Share on other sites More sharing options...
mjcnyx Posted January 11, 2012 Author Share Posted January 11, 2012 Definitely looks like it should work, but it doesn't for some reason. Link to comment Share on other sites More sharing options...
mjcnyx Posted January 11, 2012 Author Share Posted January 11, 2012 I found a solution: <script type="text/javascript">var arrayFromPHP = <?php echo json_encode($arr); ?></script> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.