PHP two-dimensional array sorting simple implementation method, PHP two-dimensional array sorting _php tutorial

Source: Internet
Author: User

PHP two-dimensional array sorting simple implementation method, PHP two-dimensional array sorting


In this paper, the simple implementation method of PHP two-dimensional array ordering is described. Share to everyone for your reference, as follows:

function Multi_compare ($a, $b) {  $val _arr = Array (      ' gold ' = ' asc ',      ' silver ' = ' desc '///can also add additional sorting criteria  );  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 ');p Rint_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 related content readers can view the topic: "Summary of PHP sorting algorithm", "PHP Basic Grammar Introductory Tutorial", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • A detailed approach to the problem of two-dimensional array sorting in PHP
    • How to sort multidimensional arrays in PHP Array_multisort
    • PHP dedicated array sorting class Arraysortutil usage instance
    • How PHP sorts an array by using the sort () function
    • PHP implementation of the specified field of multi-dimensional array sorting function sharing
    • An example of array sorting using PHP selection sorting
    • Example of array sorting using PHP insertion sorting
    • PHP array sorting usort, uksort, and sort function usage
    • Example of sort, asort, and ksort usages of PHP array ordering
    • Two-dimensional array ordering for PHP instance sharing
    • PHP two-dimensional array sorting 3 ways and custom function sharing
    • A function sharing of a PHP two-dimensional array ordering
    • PHP two-dimensional array sorting method (Array_multisort usort)
    • PHP two-dimensional array sorting detailed

http://www.bkjia.com/PHPjc/1099062.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099062.html techarticle php Two-dimensional array sorting simple implementation method, PHP two-dimensional array sorting This paper describes the simple implementation of PHP two-dimensional array ordering. Share to everyone for your reference, specifically as follows: Func ...

  • 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.