I have got my self confused on how to construct arrays correctly in PHP prior to my json encode to check them in javascript.
I’m trying to store an array of objects with their grid reference (x,y)
So i do this in php:
$get = mysql_query("SELECT x,y,sid FROM $table WHERE uid='1'") or die(mysql_error());
while($row = mysql_fetch_assoc($get)) {
$data[$row['x'].$row['y']] = $row['sid'];
}
//print_r($data);
$data = json_encode($data);
In javascript i then try to check if a object exists on a given co-ordinate so i try this:
for (i=0;i<tilesw;i++){ //horizontal
for (j=0;j<tilesh;j++){ // vertical
if(sdata[i.j]){
alert(sdata[i.j][sid]);
}
}
}
sdata is my array after json encode.
My json encode looks like this:
{"44":"0","21":"0"}
Problem is i get :
Uncaught SyntaxError: Unexpected token ] on the alert line.
Also is my approach correct or is there a better way to construct my array?
You have a JavaScript syntax error in your code. There is an extra
]on youralert()line.Should be
If you’re actually trying to concatenate the values
iandjyou also need to be using+rather than., so you would usei.toString()+j.toString()as the key rather thani.j.Example of using this with two-dimensional arrays:
PHP
JavaScript