2016-07-12 12 views
-1

Ich habe diesen Code, um alle Daten aus der DatenbankPHP-Schleife Array JavaScript-Array

while($row = mysqli_fetch_array($query)){       
$therow1 = $row['r1']; 
$therow2 = $row['r2']; 
$therow3 = $row['r3'];//get 
$therow4 = $row['r4'];//get 

} 

holen Wie kann ich die $therow3 und $therow3 als Array erhalten dann als Array auf Javascript passieren.

Die erwartete Ausgabe in JavaScript-Array wäre: $ the_array

[ 
    [-33.890542, 151.274856], 
    [-33.923036, 151.259052], 
    [-34.028249, 151.157507], 
    [-33.80010128657071, 151.28747820854187], 
    [-33.950198, 151.259302] 
    ]; 

Dann werde ich dieses Array verwenden in Javascript

<script type="text/javascript"> 
    var locations = $the_array; 

</script> 
+0

$ the_array [$ therow3] = $ row [ 'r3']; –

+0

http://php.net/manual/en/language.types.array.php – Andreas

+0

Mögliche Duplikate von [Wie übergeben Sie Variablen und Daten von PHP zu JavaScript?] (Http://stackoverflow.com/questions/23740548/) How-to-Pass-Variablen-und-Daten-von-PHP-zu-Javascript) – aghidini

Antwort

0

Bitte versuchen Sie dies:

PHP:

while($row = mysqli_fetch_array($query)){       
    $therow1 = $row['r1']; 
    $therow2 = $row['r2']; 
    $therow3 = $row['r3'];//get 
    $therow4 = $row['r4'];//get 
    $the_array[] = [$therow3, $therow4]; 
} 

Javascript:

<script type="text/javascript"> 
    var locations = JSON.parse('<?php echo json_encode($the_array); ?>'); 

</script> 
+0

Das gibt mir nur ["1", "20", "10", "11", "10", "120"] , sollte [[1,20], [10,11], [10,120]] sein –

+0

Okey! Ich habe meine Antwort aktualisiert. Bitte überprüfen Sie sie. –

0

Um ein Array in einem Format zu senden JavaScript versteht, Sie müssen es als JSON in PHP codieren und dann im Javascript

<?php 
    $the_array = json_encode([ 
     $row['r3'], $row['r4'] 
    ]); 
?> 

<script type="text/javascript"> 
    var locations = JSON.parse('<?=$the_array?>'); 
</script> 
dekodieren