Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:00:26+00:00 2026-05-31T10:00:26+00:00

I have a problem with my arrays in JavaScript. I can’t seem to get

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:00:27+00:00Added an answer on May 31, 2026 at 10:00 am

    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
        }
    }
            ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript array which defines an infinite tree like this. z=['~GROUPHEAD 0~',
I realized that I can have problems with single quotes in php arrays: <?php
I have some problem to order an array by a field of this, here
I have a problem with Javascript Literal functions. for (curitem in array) { var
I have a JavaScript array as below which I need to filter to get
I need help to solve this problem: I have a csv file like this
I seem to have a problem with objects' properties' scope. I would like to
I have a javascript -> c# array parser that I would like to feed
I have a problem using JSON and arrays. Here is my code: while($row =
I have a problem initializing an array whose size is defined as extern const.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.