0

I have a function return an associate numeric key array:

public static function getSortedFruits()
{
$fruits = array('100' => 'lemon', '102' => 'orange', '103' => 'apple', '204' => 'banana');
asort($fruits);
print_r($fruits); // return sorted array('103' => 'apple', '204' => 'banana', '100' => 'lemon', '102' => 'orange')
return $fruits;
}

i call this function from PHP code, the array is sorted

$fruits = getSortedFruits(); // sorted array

when I call this function from ajax, the array is the same as before, isn't sorted

$('#fruits').bind('change', function() {
        $.ajax({
            type: 'POST',
            url: '/ajax/getFruits', // route to getFruits function
            dataType: 'json',
            success: function(result) {
               console.log(result); // the array isn't sorted
            });
});

If the key of the $fruits is not numeric, such as a, b, c, the result is sorted as normally both function call and ajax request.

2
  • How is ajax getting generated ? Commented Apr 12, 2014 at 2:21
  • 1
    I think you should use echo json_encode($fruits);. Commented Apr 12, 2014 at 2:23

2 Answers 2

1

asort method sorts based on array values. There is no difference in order from outputs from print_r and json_encode.

$fruits = array('1' => 'lemon', '2' => 'orange', '3' => 'apple', '4' => 'banana');
asort($fruits);

print_r($fruits);
// Above outputs:Array ( [3] => apple [4] => banana [1] => lemon [2] => orange )

echo json_encode($fruits);
// Above outputs: {"3":"apple","4":"banana","1":"lemon","2":"orange"}
Sign up to request clarification or add additional context in comments.

5 Comments

I believe the poster wants to sort by array values as asort does.
With asort I am getting correct result in print_r and json_encode. So what exactly is the problem?
Updated answer, with outputs. Please remove downvote!
if I return json_encode, the javascript get sorted array but when I parse the result to javascript object, the array is autosorted by key. success: function(result) { console.log(result); // sorted by value var result2 = JSON.parse(result); console.log(result2); // sorted by key AUTOMACTICALLY. }
If you need sorting based on value, you already have it through first method. If using jQuery, no need to use JSON.parse, as data is already parsed in json object. Makes sense ?
0

I have tested your code on my local pc with xampp, you will have to use

print_r($fruits)

Or

echo json_encode($fruits);

Instead of

return $fruits

UPDATED

encode the array after returned function getSortedFruits(), something like

$fruits = getSortedFruits(); // sorted array
json_encode($fruits)

3 Comments

This function is used in both ajax request and call from other function. so I can't return json_encode
I've updated my code. Sorry if it wasn't clear for u all
the error is: when i pass a json array with numeric key to javascript, it automatically convert all key to integer and automatically sort array by key. So the sorted array is sorted again. json: {"5":"apple", "3":"lemon"} is automatically converted to {3: "lemon", 5: "apple"}. It's not my expectation.

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.