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.