Home > Article > Web Front-end > Analysis of the order of data returned by ajax request
This article mainly introduces the order of data returned by ajax request, and analyzes the ordering of ajax request return value in the form of examples. Friends in need can refer to it
This article analyzes the order of ajax request with examples The order of returning data is an issue. Share it with everyone for your reference, the details are as follows:
ajax requests a url, and after PHP backend processing, the array is in the following format:
$a = array( '-1'=> 10 ,'-3' => 2, '0' => '5' ,'-2' => 4);
Then use PHP's asort function to sort the array according to value After sorting in ascending order, it is as follows:
$a = array('-3' => 2, '-2' => 4,'0' => '5', '-1'=> 10 );
The return value received by the front-end ajax is still unordered.
Possible reasons are: because the key value is character replacement, js reorders the data
The solution is as follows:
$i = 0; foreach ($data as $k => $v) { $tmp[$i]['data'] = $v; $tmp[$i]['key'] = $k; $i++; }
The data at this time is as follows:
{ "rows": [ { "data": "2", "key": 0-3 }, { "data": "4", "key": -12 }, { "data": "5", "key": 0 }, { "data": "10", "key": -1 } ] }
ajax is received and processed, and the data is correct.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax implements page loading and content deletion
Implements ajax pop-up login function in ECSHOP
Ajax verification for duplicate implementation code
##
The above is the detailed content of Analysis of the order of data returned by ajax request. For more information, please follow other related articles on the PHP Chinese website!