Home  >  Article  >  Web Front-end  >  Ajax request operation returns data order

Ajax request operation returns data order

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 16:30:192063browse

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, 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 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 data

The 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

##How to use the ajax.load() method in jQuery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn