A detailed approach to the problem of two-dimensional array sorting in PHP

Source: Internet
Author: User

PHP in two-dimensional array ordering, you can use the PHP built-in function Uasort ()

Example one:

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

The callback function is as follows: Note that the return value of the callback function is negative or false, indicating that the first parameter of the callback function is before, and the second parameter is arranged after

?
12345678910 $person = array(  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),);//负数或者false表示第一个参数应该在前function sort_by_name($x,$y){  return strcasecmp($x[‘name‘],$y[‘name‘]);}

Use the following:

?
1 uasort($person,‘sort_by_name‘);

Here is a two-dimensional array ordering method for reference and interview use:

?
1234567891011121314151617 //$array 要排序的数组//$row  排序依据列//$type 排序类型[asc or desc]//return 排好序的数组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 Asort, Ksort, and other methods of process sequencing, relatively simple. How does the ordering of the two-dimensional arrays be implemented? Use Array_multisort and usort to achieve

For example, an array like the following:

The code is as follows:

?
12345 $users = array(  array(‘name‘ => ‘tom‘, ‘age‘ => 20)  , array(‘name‘ => ‘anny‘, ‘age‘ => 18)  , array(‘name‘ => ‘jack‘, ‘age‘ => 22));

I want to be able to sort by age from small to large. The author has collated two methods to come out, shares to everybody.

1. Using Array_multisort

Using this method, it would be more troublesome to extract the age to be stored in a one-dimensional array, and then in ascending order. The specific code is as follows:

The code is as follows:

?
12345 $ages = array (); foreach ( $users as Code class= "PHP variable" > $user    $ages [] = $ User [ ]; } array_multisort ( $ages $users

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

The code is as follows:

?
1 array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users);

2. Using Usort

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

The code is as follows:

?
1234567 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. A $ A, $b can be understood as an element under the $users array, you can directly index the name value, calculate the length, and then compare the length.

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

Here, by the way, a few functions of PHP ordering

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

Rsort the inverse of the array and the sort usage

asort sorting an array and keeping the index relationship sorted for values, generally for one-dimensional arrays, preserving index relationships

arsort the array in reverse order and keeps the index relationship consistent with the Asort usage

Ksort sorting arrays by key name

Krsort an array in reverse order by key name

A detailed approach to the problem of two-dimensional array sorting in PHP

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.