PHP array til Java Script
Hej
Jeg prøver at læse data fra en fil, nanobird.txt, i PHP og overføre 3 arrays fra PHP til Java Script. Data jeg indlæser ser ud som følgende:
3657234_3899171_3900084
-3882454_3656704_-3900094
3882850_3657135_3899640
3882319_-3656804_3899870
3882862_3657234_3899171
Min kode ser ud som følgende:
<?php
$handle = @fopen("http://myhomepage.dk/nanobird.txt", "r");
if (file_exists($handle)) {
echo "The file $filename exists";
$i=0;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo "Number: ".$i." ". $buffer.'<br>';
$array[$i] = split('_',$buffer);
$x[$i]=$array[$i][0];
$y[$i]=$array[$i][1];
$z[$i]=$array[$i][2];
//x y x
//echo "$Inserted: ". $result.'<br>';
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
print_r($array);
echo "$x: ".'<br>';
print_r($x);
echo "$y: ".'<br>';
print_r($y);
echo "$z: ".'<br>';
print_r($z);
}
?>
<script type="text/javascript">
$("input.dataUpdate").click(function () {
var i ="<?php echo ($i); ?>";
//Make 3 arrays size of i
var x = new Array();
var y = new Array();
var z = new Array();
//Transfer array
x ="<?php echo ($x); ?>";
y ="<?php echo ($y); ?>";
z ="<?php echo ($z); ?>";
for(var i=0;i<x.length;i++){
document.write("<b>arr["+i+"] is </b>=>"+x[i]+"<br>");
}
for(var i=0;i<y.length;i++){
document.write("<b>arr["+i+"] is </b>=>"+y[i]+"<br>");
}
for(var i=0;i<z.length;i++){
document.write("<b>arr["+i+"] is </b>=>"+z[i]+"<br>");
}
});
</script>
Java Script output ser ud som følgende:
arr[0] is =>A
arr[1] is =>r
arr[2] is =>r
arr[3] is =>a
arr[4] is =>y
arr[0] is =>A
arr[1] is =>r
arr[2] is =>r
arr[3] is =>a
arr[4] is =>y
arr[0] is =>A
arr[1] is =>r
arr[2] is =>r
arr[3] is =>a
arr[4] is =>y
The Value of i: [5]
Er der nogen der ved hvad jeg gør galt i overførelsen af PHP arrayet til Java Script ?