0

I have a problem with my arrays in JavaScript. I can't seem to get the value correctly.

I create my array in PHP like this:

$data = Array();        
$get = mysql_query("SELECT x,y,sid FROM table WHERE uid='1'");
    while($row = mysql_fetch_assoc($get)){
        $data[$row['x']] = Array();
        $data[$row['x']][$row['y']] = $row['sid'];
        }
$data = json_encode($row)

EDIT The json_encode comes out as "false" /EDIT

I then assigned this $data to a JS variable as sdata.

So then i try to get the value in JS but its not working. I get an undefined error.

This is my Javascript:

var i = 1;
var j = 5;
    if(sdata["x"] == i && sdata["y"] == j){
    alert(sdata["x"]["y"]["sid"]);
            }

Its meant to alert me the value of "sid" but i get: Undefined

Any ideas where my mistake is?

1
  • you have to use sdata.x, sdata.y and sdata.sid and not sdata[x] etc. Please read my answer below and please let me know if it works for you Commented Mar 14, 2012 at 0:23

5 Answers 5

2

Given your json

alert(sdata["x"]["y"]["sid"]);

should be

alert(sdata["sid"]);

as the json encoded data you show is only a one dimensional array

EDIT

If your json comes out as false, that's a different story:

$data = json_encode($row);

Should be inside your loop, for good practice, and if you're just encoding the $row why even bother with

    $data[$row['x']] = Array();
    $data[$row['x']][$row['y']] = $row['sid'];

Otherwise try

print_r($row);

within your while statement and make sure your expected result is coming through from mysql in the first place.

EDIT

If you are trying to get the $data variable in json then use

$data = json_encode($data);

outside of your loop.

EDIT

JSFiddle to return sid for given x and y - I'm sure it can be done in a cleaner way, but it's a start http://jsfiddle.net/HWByj/

var sdata = {"4":{"4":"1"},"7":{"1":"0"}};
var i = 4;
var j = 4;

if(typeof sdata[i] != 'undefined')
{
    if(typeof sdata[i][j] != 'undefined')
    {
        alert(sdata[i][j]); //gives you the value of sid
    }
}
        ​
Sign up to request clarification or add additional context in comments.

8 Comments

But there could be many rows of data thats why its in a while loop it just so happens in this situation i had one row of data but there could be numerous
See my last edit - json_encode doesn't support multi-dimensional arrays
@ErikKettenburg json_encode works just fine with multi-dimensional arrays. Where did you get that from?
@Crashspeeder you're completely right - I've removed that comment entirely
this fixed the array issue - i now have : {"4":{"4":"1"},"7":{"1":"0"}} But i need to get the value of "1" for example if x is 4 and y is 4 .. which is where im stuck :) As my JS showned above im lost how to check
|
2

In your example, you only need:

alert(sdata["sid"]);

Comments

1

If your json_encode() is returning false then make sure you're getting data back from the query. Additionally, you're not doing anything with $data, you're doing the json_encode() on $row, which will give you a different structure than I believe you're expecting.

Comments

1

It looks to me like your problem is here:

$data = json_encode($row)

I think you want this:

$data = json_encode($data);

You're looping through the rows and putting data in $data, but then you're encoding the $row variable. You're also using that $row variable to drop out of the while loop, so $row is probably null when you encode it. That's why it's coming out as false.

Comments

0

You have to use sdata.x, sdata.y and sdata.sid since the returned json is not an associative array but it's an object.

Try with this piece of js code:

function myobjtool(myobj, i, j) {
        // alert("myobj: " + myobj.x);
        if(myobj.x == i && myobj.y == j) {
                alert(myobj.sid);
        }
}

myobj = {"x":"4","y":"4","sid":"1"}
myobjtool(myobj, 4, 4)

Comments

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.