0

I am getting JSON from PHP and using it in JavaScript to put Select Box option value. But when i am putting my PHP side JSON in JavaScript it is sorting automatically.

This should not be done.

Actually i am using this JSON as HTML select Box option value and i need my original data as coming From PHP side.

JSON Coming from PHP side:

PHP CODE:

$optionJSON= json_encode($optionValues);

Result:

{
    "0":"Select",
    "37":"Abc",
    "47":"DEF",
    "359":"GHI",
    "182":"JKL",
    "360":"MNO",
    "183":"PQR",
    "320":"STU",
    "38":"VWX",
    "80":"YZA"
}

Showing In browser console and same order in Select Box Option value.

{
    "0": "Select",
    "37": "Abc",
    "38": "VWX",
    "47": "DEF",
    "80": "YZA",
    "182": "JKL",
    "183": "PQR",
    "320": "STU",
    "359": "GHI",
    "360": "MNO"
}
3
  • This is not regulated behaviour by ECMA. You can not rely on having this browser-independent. %)p Commented Aug 23, 2014 at 11:09
  • 1
    The order of elements in an object is not specified, and there's no way to control it. If you want the values to stay in a particular order, use an array. Commented Aug 23, 2014 at 11:10
  • @Barmar Please can you give me a example Commented Aug 23, 2014 at 11:11

2 Answers 2

1

You pass a JSON object around, within the object instance you cannot rely on the order of the elements.

Use an array instead or give each object some kind of sort order and sort the list on the client...

var x = [
  {"0":"Select"},
  {"37":"Abc"},
  {"47":"DEF"},
  {"359":"GHI"},
  {"38":"VWX"}
];
Sign up to request clarification or add additional context in comments.

1 Comment

The JSON comes from PHP, so it would be better to show the PHP syntax of this data.
1

Return an array like this:

$result = array(
    array('value' => 0, 'text' => 'Select'),
    array('value' => 37, 'text' => 'Abc'),
    array('value' => 47, 'text' => 'DEF'),
    array('value' => 359, 'text' => 'GHI'),
    array('value' => 182, 'text' => 'JKL'),
    array('value' => 360, 'text' => 'MNO'),
    array('value' => 183, 'text' => 'PQR'),
    array('value' => 320, 'text' => 'STU'),
    array('value' => 38, 'text' => 'VWX'),
    array('value' => 80, 'text' => 'YZA')
);
echo json_encode($result);

When I do this, the output is:

[{"value":0,"text":"Select"},{"value":37,"text":"Abc"},{"value":47,"text":"DEF"},{"value":359,"text":"GHI"},{"value":182,"text":"JKL"},{"value":360,"text":"MNO"},{"value":183,"text":"PQR"},{"value":320,"text":"STU"},{"value":38,"text":"VWX"},{"value":80,"text":"YZA"}]

When I copy that to Javascript and use JSON.parse(), the result is in the desired order. See http://jsfiddle.net/barmar/8ypzvhku/1/

1 Comment

Sorry Mr. Barmar Problem is same.. :(

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.