This question has already been answered:

There are 2 one-dimensional PHP arrays and 1 two-dimensional JS array

PHP arrays:

$GA_lat[] = [-35.165034,35.696121,-36.113032,-36.852924,-36.868273] $GA_lng[] = [174.162854,174.300132,174.559536,174.750234,174.711450] 

You need to move data from PHP arrays to a two-dimensional JS array. (Data from cell 0 to 0 cell of a two-dimensional array, etc.)

What would happen:

 var locations = [ ['1', -35.165034, 174.162854, 5], ['2', -35.696121, 174.300132, 4], ['3', -36.113032, 174.559536, 3], ['4', -36.852924, 174.750234, 2], ['5', -36.868273, 174.711450, 1] ]; 

Reported as a duplicate by Air members, Suvitruf , 0xdb , br3t , slippyk Mar 27 '18 at 12:44 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

On the server, convert the data to json

  $GA_lat = [-35.165034,35.696121,-36.113032,-36.852924,-36.868273]; $GA_lng = [174.162854,174.300132,174.559536,174.750234,174.711450]; $transformData = []; $coumtGA = count($GA_lat); for ($i = 0; $i < $coumtGA; $i++) { $transformData[] = [strval($i+1), $GA_lat[$i], $GA_lng[$i], $coumtGA-$i]; } echo json_encode($transformData); 

On the client, we get json (for example, via ajax or write it somewhere in the field) and convert it to an object:

 var dataFromServer = JSON.parse(answerFromServer);