A simple method to realize php two-dimensional array sequencing _php skills

Source: Internet
Author: User

In this paper, we describe a simple method to realize php two-dimensional array sorting. Share to everyone for your reference, specific as follows:

function Multi_compare ($a, $b)
{
  $val _arr = Array (
      ' Gold ' => ' ASC ',
      ' silver ' => ' desc '// Additional sorting criteria can also be added
  );
  foreach ($val _arr as $key => $val) {
    if ($a [$key] = = $b [$key]) {
      continue;
    }
    Return (($val = = ' desc ') -1:1) * (($a [$key] < $b [$key]) -1:1);
  return 0;
}
$arr = Array (
  ' Gold ' =>1, ' Silver ' =>2),
  array (' Gold ' =>8, ' Silver ' =>10),
  Array (' Gold ' =>8, ' Silver ' =>8),
  array (' Gold ' =>2, ' Silver ' =>1),
);
Uasort ($arr, ' multi_compare ');
Print_r ($arr);

The results of the operation are as follows:

Array
(
  [0] => array
    (
      [gold] => 1
      [silver] => 2
    )
  [3] => array
    (
      [ Gold] => 2
      [silver] => 1
    )
  [1] => Array
    (
      [gold] => 8
      [silver] =>
    )
  [2] => Array
    (
      [Gold] => 8
      [silver] => 8
    )
)

More about PHP Interested readers can view the site topics: "PHP Sorting algorithm Summary", "PHP basic Grammar Introductory Course", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"

I hope this article will help you with the PHP program design.

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.