Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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
 
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 Feb 27 '11 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 Feb 27 '11 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 Feb 27 '11 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 Feb 27 '11 at 21:34

3 Answers

up vote 1 down vote accepted

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);?>");
share|improve this answer
 
thanks thanks ........................really thanks Kristian J :) –  Tirupati Balan Feb 27 '11 at 21:34

Try

var longitudeArray=<?=json_encode($longitudeArray)?>;
share|improve this answer
 
but why i use json_encode –  Tirupati Balan Feb 27 '11 at 21:28
 
JSON=JavaScript Object Notation~ it takes a PHP array and converts it to native JavaScript code~ –  Shad Feb 27 '11 at 21:29

@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"}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.