0

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?

6
  • 2
    What is i.j attempting to accomplish? This is invalid javascript syntax. Commented Mar 8, 2012 at 22:05
  • i and j are two variables created in the for loop so then i check if: sdata[10] is true.. is so echo the value of sdata[10]. This is an example if i was 1 and j was 0 Commented Mar 8, 2012 at 22:07
  • Ahh okay so you are using the PHP string concatenation syntax. In javascript you would want to do sdata[i.toString() + j.toString()] (or some reasonable facsimile.) Commented Mar 8, 2012 at 22:10
  • Wouldn't if(sdata[i+j]) be sufficient ? Or will that create a sum ? Commented Mar 8, 2012 at 22:12
  • correct if they are both numeric types then it would apply the addition operation you could do sdata[i + '' + j] ..any addition operation with a string will cause all subsequent addition operations to be string concatenations regardless of type. Commented Mar 8, 2012 at 22:15

1 Answer 1

1

You have a JavaScript syntax error in your code. There is an extra ] on your alert() line.

alert(sdata[i.j][sid]]);   

Should be

alert(sdata[i.j][sid]);  

If you're actually trying to concatenate the values i and j you also need to be using + rather than ., so you would use i.toString()+j.toString() as the key rather than i.j.

Example of using this with two-dimensional arrays:

PHP

$arr = array();
while($row = mysql_fetch_assoc($get)) {
    if(!isset($arr[$row['x']])) $arr[$row['x']] = array();
    $arr[$row['x']][$row['y']] = $row['sid'];
}

$data = json_encode($arr);

JavaScript

for(var x in sdata) {
    for(var y in sdata[x]) {
        alert('Object found at coordinates ' + x + ',' + y + ' ' + sdata[x][y]);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ah yeh i fixed that mistake :) thanks - though it still does not seem to alert me once i and j meet an array =/
Ok i have edited that yet no alert occurs =/ the json encode suggests there is one at i 4 and j 4 but doesn't seem to create the alert =/
@Dave I neglected to take into account that i and j were integers. I've updated my answer once more.
Thanks jprofitt ill tick you're answer as it is now working... on a side note - is my method most efficient to the best of you're knowledge?
I would potentially use a two-dimensional array like array($row['x'] => array($row['y'])); as the return object so you can simply iterate over the values in the array to determine which cells have objects rather than brute forcing it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.