How to assign function to an Array of Objects?

3

I have this php function returning this String:

function retornaGeoCod(){

    for($i = 0; $i < 2; $i++){
        echo '{
            "lat":"-19.4746845", 
            "long":"-44.159503",
            "local":"Prudente de Morais"},';
    }
}

How do I insert this function into an array of JavaScript objects?

var array = [{}];

I used json_encode as @ShutUpMagda suggested, but now my console has the following error:

  

SyntaxError: expected expression, got '>' var contentString =   string $ this- > sc_temp_i.local;

I'm using google maps api, I'm doing it this way:

var cord = [ <?=$this->array_push(retornaGeoCod());?> ]; 

But the map does not appear: /

    
asked by anonymous 30.11.2016 / 11:14

1 answer

3

Use json_encode ( manual ):

<?php

function retornaGeoCod() {
    $array = [];
    for($i = 0; $i < 2; $i++){
        $array[$i] = [
            "lat" => "-19.4746845", 
            "long" => "-44.159503",
            "local" => "Prudente de Morais"
        ];
        return $array;
    }
}

echo 'var array = '.json_encode(retornaGeoCod());
    
30.11.2016 / 11:41