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

  • SEARCH
  • Home
  • 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 7888449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:50:03+00:00 2026-06-03T05:50:03+00:00

Problem: returns array, but not in json. Result: must return array with in array.

  • 0

Problem: returns array, but not in json.

Result: must return array with in array. outer array has key with numbers and value is array with keys “ID” and “NAME” and there values are assigned from database.

$i = 0;
$json_values = array();
while($sctg = mysql_fetch_assoc($get_sctg)){
    $json_values[$i] = array("ID" => $sctg['ID'], "NAME" => $sctg['NAME']);
    $i++;
}
echo json_encode($json_values);
  • 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-06-03T05:50:05+00:00Added an answer on June 3, 2026 at 5:50 am

    Your code is perfectly fine – you just misunderstood the different between arrays and objects in JavaScript.

    [{"ID":"4","NAME":"asdasd"},
     {"ID":"3","NAME":"LOKS"},
     {"ID":"1","NAME":"LOL"}]
    

    This is JSON containing an array with three elements. Each element is an object with the properties ID and NAME.

    Let’s assume that array is stored in data. You can iterate over the objects in that array with a simple for loop:

    for(var i = 0; i < data.length; i++) {
        var row = data[i];
        // Here you can use row.ID and row.NAME
    }
    

    I guess you expected your JSON to look like this:

    {
        0: {"ID":"4","NAME":"asdasd"},
        1: {"ID":"3","NAME":"LOKS"},
        2: {"ID":"1","NAME":"LOL"}
    }
    

    This would actually be bad since objects in JavaScript are not ordered (even though they actually are in most browsers, but that’s not guaranteed!). So when iterating over the rows in such an element (using for(var key in data)), you would not necessarily get the row with ID=4 first just because its key is zero.

    However, if you really want an object instead of an array for some reason (you don’t!), you could always cast the array to object:

    echo json_encode((object)$json_values);
    

    As a side-note, usually it’s a good idea to have an object as the top-level element in JSON for security reasons (you can redefine the array constructor and then include something with a top-level array with a script tag and thus access data usually protected by the same origin policy if it was accessed by an XHR request), so I’d change the PHP code like this:

    echo json_encode(array('rows' => $json_values));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django python problem: The select drop down returns the numeric value of the item.
I have the following problem. The alert allways returns undefined, but I know it
My problem: I have an JSON Array containing arrays, which i render into the
I am using a jQuery autocomplete script but the problem is I do not
The problem is, I get some parts of the contents but did not get
The topic says it all: I'm having a problem where MessageBox returns immediately (without
Here's my problem (for en-US): Decimal.Parse(1,2,3,4) returns 1234, instead of throwing an InvalidFormatException. Most
The following code returns Exit code 58. From cURL documentation: CURLE_SSL_CERTPROBLEM (58) problem with
I have a problem with the SQL statement detailed below. The query returns the
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .

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.