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 6585053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:50+00:00 2026-05-25T16:36:50+00:00

I am trying to convert a MySQL query result into JSON within an AJAX

  • 0

I am trying to convert a MySQL query result into JSON within an AJAX request.
My code looks like this at the moment.

$offset              = empty($_GET['offset'])   ? 0 : $_GET['offset'];
$numimagestodisplay = 3;    
$items              = array();
$allitems           // This is the queryset obtained through a call to a function

foreach ($allitems as $i => &$item)
{
    if (($i >= $offset) && (count($items) < $numimagestodisplay))
    {
        $items[$i] = $item;
    }       
}

$output = '{"items":'.json_encode($items).'}'; 

I then want to cycle through the returned results in the javascript calling the above code and need to refer to the array items by their keys (I need to alter some HTML element IDs using these values). However, the JSON is returned in the wrong format.

If I change the line

$items[$i] = $item;

to:

$items[] = $item;

Then I can refer to it by key, however the keys are obviously just 0, 1, 2, whereas I need the key to be the value defined in the loop.

How can I alter the PHP code to return the JSON in the correct format?

Any advice appreciated.

Thanks.

  • 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-25T16:36:50+00:00Added an answer on May 25, 2026 at 4:36 pm

    The problem is that arrays in Javascript (and most other languages for that matter) can’t have user defined keys. You want your array to be encoded to a JSON object instead of an array (arrays in PHP with user-defined keys are in essence objects). This usually happens automatically, for arrays with non-numeric keys.

    In your case, you can use the JSON_FORCE_OBJECT flag:

    $output = '{"items":'.json_encode($items,JSON_FORCE_OBJECT).'}';
    

    From the documentation:

    Non-associative array output as array: [[1,2,3]] 
    Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert this MSSQL QUERY to MYSQL Query looks like this
Trying to convert this c code into MIPS and run it in SPIM. int
In MSSQL you can convert a string into an integer like this: CONVERT(INT, table.column)
Part of the MySQL query that I'm trying to convert to pgSQL : LEFT
Okay I'm trying to convert MySQL request to PDO MySQL. I already do that
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
I am trying to extract unix timestamp from mysql and convert it to ISO8601
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
I'm trying to convert some code that worked great in VB, but I can't
I am trying to convert an ASP.NET website into a web application project. The

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.