admin管理员组

文章数量:1023195

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.

Share Improve this question edited Feb 27, 2011 at 22:39 Fran Verona 5,4766 gold badges48 silver badges87 bronze badges asked Feb 27, 2011 at 21:15 Tirupati BalanTirupati Balan 6741 gold badge10 silver badges29 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 5

Try

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

Share Improve this question edited Feb 27, 2011 at 22:39 Fran Verona 5,4766 gold badges48 silver badges87 bronze badges asked Feb 27, 2011 at 21:15 Tirupati BalanTirupati Balan 6741 gold badge10 silver badges29 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 5

Try

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