How to assign a function to an Object Array?

Asked

Viewed 112 times

3

Guys 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 enter this function into an object array in Javascript?

var array = [{}];

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

Syntaxerror: expected Expression, got '>' var contentString = Cord$this->sc_temp_i.local;


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

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

But the map doesn’t appear :/

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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.