admin管理员组文章数量:1429935
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.
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.
- And how would you like it solved, should it fail with a descriptive error or should it convert one to 1 and two to 2 or what? – David Mårtensson Commented Feb 27, 2011 at 21:19
- it is not showing any result and if if i read longitudeArray[0] then its not showing value at position 0 – Tirupati Balan Commented Feb 27, 2011 at 21:25
- Are you seriously saying your code is spelling the numbers out? Or do you mean they're just being interpreted by JavaScript as Strings, even though they're still numeric, e.g. ("12.32444", "21.34343", "23.5454")?!?! – Lee Kowalkowski Commented Feb 27, 2011 at 21:31
- ...and how is it not being created properly? Because JavaScript is pretty good at understanding Strings containing a number, the confusion usually starts when addition results in concatenation. – Lee Kowalkowski Commented Feb 27, 2011 at 21:34
3 Answers
Reset to default 5Try
var longitudeArray=<?=json_encode($longitudeArray)?>;
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);?>");
@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 acplished 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"}
本文标签: How to read php array of string in javascriptStack Overflow
版权声明:本文标题:How to read php array of string in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745553629a2663057.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论