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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:14:28+00:00 2026-06-05T03:14:28+00:00

First this is not a duplicate questions. I’ve looked through some similar problem and

  • 0

First this is not a duplicate questions. I’ve looked through some similar problem and most of the answer is what I am using right now.

Here is the problem set up,
on PHP side

$array = array('name' => 'a', 'data' => array('0'=>15,'0.25'=>'18','0.35'=>19,'1' =>20));
echo json_encode($array);

on the JS side

data = $.parseJSON(data); // data is the return from the php script
above

As you can see the $array[‘data’] is an associative array with numeric number as its key and sorted in order. While parsing into JSON, javascript altered the order of that array and sorted 0 and 1 as numeric key and put them to the head of the object.

I know this is standard behavior for certain browser such as chrome, and IE9.
I’ve read somewhere that people suggest stick with array strictly if I want to maintain the order of the array.
But my question is how do you feed back an array from PHP to javascript as an array instead of using json object? Or is there other solution to this kind of problem . Thanks for the input in advance.

Thanks for the input in advance

  • 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-05T03:14:30+00:00Added an answer on June 5, 2026 at 3:14 am

    Use an array to maintain order, and then an object to create the map. There are two ways. I would suggest:

    $array = array('name' => 'a', 'data' => 
      array(
        array('key' => 0, 'value' => 15),
        array('key' => 0.25, 'value' => 18),
        array('key' => 0.35, 'value' => 19),
        array('key' => 1, 'value' => 20),
      )
    );
    echo json_encode($array);
    

    Which will give you the JSON:

    {
        "name": "a",
        "data": [
           {"key": 0, "value": 15},
           {"key": 0.25, "value": 18},
           {"key": 0.35, "value": 19},
           {"key": 1, "value": 20}
        ]
    }
    

    Then you will have order but to look up a certain key will be more difficult. If you want that to be easy you can return a mapping object as well like this:

    $array = array('name' => 'a', 'data' => 
      array(
        "0" => 15,
        "0.25" => 18,
        "0.35" => 19,
        "1" => 20,
      ),
      'order' => array("0", "0.25", "0.35", "1")
    );
    echo json_encode($array);
    

    Which will give you:

    {
        "name": "a",
        "data": {
           "0": 15,
           "0.25": 18,
           "0.35": 19,
           "1": 20
        },
        "order": ["0", "0.25", "0.35", "1"]
    }
    

    One of these two methods of returning your data should prove to be the most useful for your specific use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent quite some time going through similar questions here and have not
First off, this is NOT a duplicate of: Turn a C string with NULL
First of all: This question is not directly programming related. However, the problem only
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
First of I'm very sorry but this questions is not so so specific. All
First of all: this is not a duplicate of Delphi and SAPI . I
First of all, this is not a duplicate of Enums in Ruby :) The
This is admittedly similar to (but not a duplicate of) Comparison of full text
First of all this is NOT an exact duplicate of Initialize final variable before
first of all: this is not the same as this . The ModelBackend has

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.