Javascript Array php values

Asked

Viewed 87 times

2

In my database I have

id , coluna1, coluna2

1  , 38.951399 ,  -76.958463

2 , 38.942855,   -76.959149

I’m using php

I need to transform this information into a Javascript array with the following structure

 new Array([38.951399, -76.958463], [38.942855, -76.959149]);

The communication between Javascript in Php is ok, in js I can print in the strings console, what I can’t and do this conversion of these strings to an Array in JS.

2 answers

3

Use json_encode():

<?php

$array = array(
    '1' => array('38.951399' ,  '-76.958463'),
    '2' => array('38.951399' ,  '-76.958463'),
);

$var = json_encode($array);

Already in the view, you print like this:

<script> var javascriptArray = <?php echo $var ?> </script>
  • neoprofit am trbalhando well like you posted...sorry but I’m inciante, I am jquery to call php so I have and can go through an array like you posted... the question is to pass this result as parameter to another function that is waiting for the following value. new Array([38.951399, -76.958463], [38.942855, -76.959149]); In php I’m returning an array array...

  • I couldn’t understand the scenario :/ How about improving the question?

  • neoprofit confused me your example worked thank you!

2


Mount the array in php first, then output it with:

echo json_encode($minhaArray);

The result will be:

[[38.951399, -76.958463], [38.942855, -76.959149]]

Browser other questions tagged

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