将AJAX响应保存在数组中

I'm having X and Y coordinates in my MySQL table. I have them in my php file putting them in an associative array

while($row=$result->fetch_assoc()){
    echo $row['x'];
    echo $row['y'];
}

I want to save them in my html file ( with javascript) in 2 Arrays. How is this possible? Can I use for example a javascript loop which switches from saving the output (the x and y coordinates) in different arrays?

Thanks for every response!

Are you looking for json_encode?
http://php.net/manual/en/function.json-encode.php

This will let you print the arrays directly into JavaScript. For instance:

<?php
    $coord = array(
        'x' => 52,
        'y' => 17,
    );
?>

<script>
var coords = <?php echo json_encode($coord); ?>;
</script>

will print

<script>
var coords = {"x":52,"y":17};
</script>

Put them into a PHP array, then convert that to Javascript using json_encode.

$array = array();
while($row=$result->fetch_assoc()){
    $array[] = array('x' => $row['x'], 'y' => $row['y']);
}
echo json_encode($array);

In the Javascript, the AJAX callback function can use JSON.parse() to convert this to an array of objects.

If you want two arrays, push the x and y values onto different arrays, then combine them in the JSON response.

$xarray = array();
$yarray = array();
while($row=$result->fetch_assoc()){
    $xarray[] = $row['x'];
    $yarray[] = $row['y'];
}
echo json_encode(array('x' => $xarray, 'y' => $yarray));

Then in the Javascript you do:

var obj = JSON.parse(response);
var xarray = obj.x;
var yarray = obj.y;