A detailed _php example of two-dimensional array sorting in PHP

Source: Internet
Author: User
Tags sorted by name

Two-dimensional array ordering in PHP, you can use PHP built-in function uasort ()

Example one:

Use the user-defined comparison function to sort the values in the array and keep the index associated

The callback function is as follows: note that when the return value of the callback function is negative or FALSE, the first parameter of the callback function is in front, and the second argument is arranged in the back

$person = Array (
  ' num ' => ' 001 ', ' id ' =>6, ' name ' => ' Zhangsan ', ' age ' =>21),
  array (' num ' => ') 001 ', ' id ' =>7, ' name ' => ' Ahangsan ', ' age ' =>23 ',
  array (' num ' => ' 003 ', ' id ' =>1, ' name ' => ') Bhangsan ', ' age ' =>23),
  array (' num ' => ' 001 ', ' id ' =>3, ' name ' => ' Dhangsan ', ' age ' =>23),
);
A negative number or false indicates that the first parameter should be in the former
function sort_by_name ($x, $y) {return
  strcasecmp ($x [' name '], $y [' name ']);
}

Use the following:

Uasort ($person, ' sort_by_name ');

A two-dimensional array ordering method for reference and interview purposes is given below:

$array array
//$row  Sort By column
//$type sort Type [ASC or DESC]
//return array
function Array_sort ($ Array, $row, $type) {
  $array _temp = Array ();
  foreach ($array as $v) {
    $array _temp[$v [$row]] = $v;
  }
  if ($type = = ' asc ') {
    ksort ($array _temp);
  } ElseIf ($type = ' desc ') {
    krsort ($array _temp);
  } else{
  } return
  $array _temp;
}

Example two:

One-dimensional array ordering can be sorted using Asort, Ksort, and other method processes, which is relatively simple. How does the order of two-dimensional arrays come true? Use Array_multisort and Usort to implement

For example, an array like the following:

The code is as follows:

$users = Array (
  ' name ' => ' Tom ', ' Age ' =>)
  , Array (' name ' => ' Anny ', ' age ' =>)
  , AR Ray (' name ' => ' Jack ', ' age ' =>)
);

Want to be able to sort by age from small to large. The author has collated two methods to come out and share to everybody.

1. Use Array_multisort

Using this method, it is more cumbersome to extract the age out of the one-dimensional array, and then arrange it in ascending order. The specific code is as follows:

The code is as follows:

$ages = Array ();
foreach ($users as $user) {
  $ages [] = $user [' age '];
}
Array_multisort ($ages, SORT_ASC, $users);

After execution, the $users is a sorted array, which can be printed out to see. If you need to first in ascending order by age, and then sorted by name ascending, the method above, is to extract an array of names, the last sort of method called:

The code is as follows:

Array_multisort ($ages, SORT_ASC, $names, SORT_ASC, $users);

2. Use Usort

The biggest advantage of using this method is that you can customize some of the more complex sorting methods. For example, by the length of the name in descending order:

The code is as follows:

Usort ($users, function ($a, $b) {
      $al = strlen ($a [' name ']);
      $BL = strlen ($b [' name ']);
      if ($al = = $BL) return
        0;
      Return ($al > $bl)? -1:1;
    });

Anonymous functions are used here and can be extracted separately if necessary. Where $a, $b can be understood as the elements under the $users array, you can directly index the name value, and calculate the length, and then compare the length can be.

=====================================================================

Here, by the way, some of the functions of PHP sort

sort array Sorting generally applies to one-dimensional indexed arrays and does not keep indexes

rsort Array Reverse ordering and sort usage consistent

asort Array to sort and maintain index relationships to sort values, generally applies to one-dimensional arrays, keeping index relationships

Arsort the array to reverse and keep the index relationship and Asort usage consistent

Ksort Array by Key name

Krsort Arrays in reverse order by key name

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.