How to solve the problem of changing the original location of usort in PHP when values are consistent _ php skills

Source: Internet
Author: User
After PHP4.1.0, the original position of usort may change when the compare values are the same. if the two compare values are the same, their order in the sorting results will be random. If you need to keep the original position of the same value, refer to the method in this article. After PHP 4.1.0 and later versions, the original position of usort may change when the compare values are the same. this is what is said in the document:
If two members compare as equal, their order in the sorted array is undefined.
That is to say, if the two values are the same, their order in the sorting result is random. If you need to keep the original position of the same value, refer to the method in this article.
Demo data:

The code is as follows:


/*
Solve the problem of changing the original position of usort in PHP at the same time.
By Artlover http://www.CodeBit.cn
*/
$ Arr = array (
Array ('a' => 5, 'B' => 3 ),
Array ('a' => 5, 'B' => 1 ),
Array ('a' => 5, 'B' => 4 ),
Array ('a' => 5, 'B' => 2 ),
);
?>


The values of the first element in the array are the same, and the expected result is to keep the existing position unchanged, that is, the order of B is 3, 1, 4, 2.
Sort by usort. when the values of the compare fields are the same, the original order may change.

The code is as follows:


/*
Solve the problem of changing the original position of usort in PHP at the same time.
By Artlover http://www.CodeBit.cn
*/
$ Callback = create_function ('$ a, $ B', 'Return ($ a ["a"] = $ B ["a"])? 0 :( ($ a ["a"]> $ B ["a"])? 1:-1 );');
Usort ($ arr, $ callback );
?>


Result:

The code is as follows:


Array
(
[0] => Array
(
[A] => 5
[B] => 2
)
[1] => Array
(
[A] => 5
[B] => 4
)
[2] => Array
(
[A] => 5
[B] => 1
)
[3] => Array
(
[A] => 5
[B] => 3
)
)


Although the values of the sorting fields are the same, usort disconnects the entire array order.
If you want to keep the original position while comparing values, you can use array_multisort:

The code is as follows:


/*
Solve the problem of changing the original position of usort in PHP at the same time.
By Artlover http://www.CodeBit.cn
*/
// Index counter
$ I = 0;
// Create two empty arrays. The first stores the fields to be sorted, and the second stores the original index information.
$ A = $ index = array ();
Foreach ($ arr as $ key => $ data ){
$ A [$ key] = $ data ['A'];
$ Index [] = $ I ++;
}
// The first array is first arranged, followed by the original index
Array_multisort ($ a, SORT_ASC, $ index, SORT_ASC, $ arr );
?>


Result:

The code is as follows:


Array
(
[0] => Array
(
[A] => 5
[B] => 3
)
[1] => Array
(
[A] => 5
[B] => 1
)
[2] => Array
(
[A] => 5
[B] => 4
)
[3] => Array
(
[A] => 5
[B] => 2
)
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.