Home > Article > Web Front-end > Ajax request operation returns data order
This time I will bring you the order of data returned by ajax request operation. What are the precautions for the order of data returned by ajax request operation? The following is a practical case, let's take a look.
ajax requests a url, and after PHP backend processing, thearray 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 in ascending order by value, As follows:
$a = array('-3' => 2, '-2' => 4,'0' => '5', '-1'=> 10 );The return value received by the front-end ajax is still out of order. The possible reason is: because the key value is character replacement, js reorders the dataThe processing plan 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 reception and processing, the data is correct. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to partially update the Razor page
The above is the detailed content of Ajax request operation returns data order. For more information, please follow other related articles on the PHP Chinese website!