search
Homephp教程php手册PHP中二维数组排序问题

PHP中二维数组排序问题

PHP中二维数组排序,可以使用PHP内置函数uasort()

【使用用户自定义的比较函数对数组中的值进行排序并保持索引关联】

回调函数如下:注意回调函数的返回值是负数或者是false的时候,表示回调函数的第一个参数在前,第二个参数在后排列

 

	$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']);
	}
使用如下:

 

 

uasort($person,'sort_by_name');

 

下面给出一个二维数组排序的方法,供参考和面试使用:

 

	//$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;
	}

 

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

这里顺便说一下PHP排序的几个函数

【sort 对数组排序】一般适用于一维索引数组,不会保持索引

【rsort 对数组逆向排序】 和sort用法一致

 

【asort 对数组进行排序并保持索引关系】对值进行排序,一般适用于一维数组,保持索引关系

【arsort 对数组进行逆向排序并保持索引关系】和asort用法一致

 

【ksort 对数组按照键名排序】

【krsort 对数组按照键名逆向排序】

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

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft