CodeIgniter Forums
How To convert the Php array to Javascript array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How To convert the Php array to Javascript array (/showthread.php?tid=64530)



How To convert the Php array to Javascript array - Chandini - 03-01-2016

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>



RE: How To convert the Php array to Javascript array - Mangetsu - 03-01-2016

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.


RE: How To convert the Php array to Javascript array - skunkbad - 03-01-2016

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.