Two-dimensional array ordering problem in PHP

Source: Internet
Author: User
Tags array arrays key return sort

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

"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;
	}

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

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 the index

"Rsort array Reverse" and sort usage consistent

Sort values by asort the array and keeping an indexed relationship, generally applies to one-dimensional arrays, keeping index relationships

"Arsort an array and keeping an indexed relationship" is consistent with asort usage

"Ksort array by key name"

"Krsort Arrays in reverse order by key name"

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



Related Article

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.