如何在javascript中读取php数组的字符串

var longitudeArray = new Array(<?php $result = count($longitudeArray);
                                    if ($result > 1){
                                    echo implode(',', $longitudeArray); 
                                    } else {
                                    echo $longitudeArray[0];
                                    }
                                    ?>);

$longitudeArray contain array of number like: $longitudeArray = array(23.54545, 2323.32); Above script create following javascript array:

var longitudeArray = new Array(12.32444,21.34343,23.5454);

but if i passes string in $longitudeArray like:

$longitudeArray = array('one', 'two');

instead of integer value in $longitudeArray then my javascript array is not creating properly or its not working.

If you pass an array of strings to your code, you will end up without quotes around them in your generated javascript code. You need to add some quotes somehow, something like:

var longitudeArray = new Array("<?php echo implode('","', $longitudeArray);?>");

Try

var longitudeArray=<?=json_encode($longitudeArray)?>;

@Shad, very useful and efficient approach. In the same manner, if one is trying to convert a PHP array to pass back to a JavaScript function (EG: an AJAX callback), that would be accomplished as such:

$some_php_array = array( 'indexA' => 'nice', 'indexB' => 'move' );
json_encode($some_php_array);

Where the PHP data would look as follows in JavaScript:

{"indexA":"nice","indexB":"move"}