Home  >  Article  >  Backend Development  >  How to sort an array based on two fields in php

How to sort an array based on two fields in php

PHPz
PHPzOriginal
2023-04-18 09:06:04922browse

In PHP, when we need to sort an array, we can use built-in functions such as sort() and rsort() to sort the elements in the array. However, sometimes we need to sort based on two fields in the array, then we need to use the usort() function.

usort — Sort values ​​in an array using a user-defined comparison function

Syntax: bool usort (array &$array, callable $value_compare_func)

This function Sorts the values ​​in an array using the specified comparison function. It should be noted that this function directly affects the original array and does not return the sorted array.

Usage scenarios

When dealing with some complex applications, sometimes it is necessary to sort by multiple fields, such as sorting by price first, and then sorting by sales volume when the price is the same, then we need Use the usort() function to achieve this.

Sample code

The following is an actual example of using the usort() function to sort an array. Suppose we have a product array that contains product name, price, sales volume and other information. Now we need to sort the products based on price and sales volume.

$products = [
   [
      "name" => "商品A",
      "price" => 100,
      "sales" => 200
   ],
   [
      "name" => "商品B",
      "price" => 120,
      "sales" => 150
   ],
   [
      "name" => "商品C",
      "price" => 80,
      "sales" => 300
   ],
];

function cmp($a, $b) {
   if ($a['price'] == $b['price']) {
      return $a['sales'] < $b[&#39;sales&#39;] ? 1 : -1;
   }
   return $a[&#39;price&#39;] > $b['price'] ? 1 : -1;
}

usort($products, "cmp");

print_r($products);

Run results:

Array
(
    [0] => Array
        (
            [name] => 商品C
            [price] => 80
            [sales] => 300
        )

    [1] => Array
        (
            [name] => 商品A
            [price] => 100
            [sales] => 200
        )

    [2] => Array
        (
            [name] => 商品B
            [price] => 120
            [sales] => 150
        )
)

Here we define a cmp() function, which is used to compare the price and sales volume of two products, and sort them according to price from small to large and sales volume from large to large. Sort in small order. Then use the usort() function to sort the product array and print the sorted results.

Summary

Using the usort() function can flexibly sort arrays and sort based on multiple fields to achieve more complex application requirements. It should be noted here that the sorting function compares two array elements, so $a and $b represent two elements in the array respectively. The function needs to return one of the three values ​​​​of positive number, negative number and 0, indicating that $a ratio $b is larger, smaller, or equal.

The above is the detailed content of How to sort an array based on two fields in php. 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