Welcome Guest, Not a member yet? Register   Sign In
How To convert the Php array to Javascript array
#1

This is My php Array
PHP Code:
Array
(
 
   [0] => Array
 
       (
 
           [location] => Pulikeshi Nagar
            
[latitude] => 77.6143056
            
[longitude] => 12.9972286
        
)

 
   [1] => Array
 
       (
 
           [location] => Pulikeshi Nagar
            
[latitude] => 77.6142942
            
[longitude] => 12.9972074
        
)

 
   [2] => Array
 
       (
 
           [location] => Pulikeshi Nagar
            
[latitude] => 77.6142742
            
[longitude] => 12.9971682
        
)

 
   [3] => Array
 
       (
 
           [location] => Shivaji Nagar
            
[latitude] => 12.997208
            
[longitude] => 77.6142774
        
)

 
   [4] => Array
 
       (
 
           [location] => 
 
           [latitude] => 12.9718915
            
[longitude] => 77.6411545
        
)

 
   [5] => Array
 
       (
 
           [location] => Cleveland Town
            
[latitude] => 12.9718915
            
[longitude] => 77.6411545
        

..  How to convert the this php array to JavaScript array  like
Code:
<script type="text/javascript">
var locations = [
['Pulikeshi Nagar',12.9972286,77.6143056],
['Pulikeshi Nagar',12.9972074,77.6142942],
['Pulikeshi Nagar',12.9971682,77.6142742]
];</script>


Attached Files Thumbnail(s)
       
Manikanta
Reply
#2

(This post was last modified: 03-01-2016, 05:41 AM by Mangetsu.)

PHP Code:
$input=array(
   array(
       'loc'=>'loc1',
       'lat'=>'32131',
       'lng'=>'23232'
       ),
  array(
       'loc'=>'loc2',
       'lat'=>'111',
       'lng'=>'23232222'
       ),
   
   
);
             
$output
=array();
foreach(
$input as $a){
$output[]=array_values($a);
}
echo 
json_encode($output); 

Send it to view or whatever and echo as JS variable.
Reply
#3

If you're actually printing the json encoded string in an attribute, and then expect JS to get that string and use it as an array or object, you'll need to use JSON.parse() in your JS.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB