Blogger Information
Blog 87
fans 0
comment 0
visits 59081
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月18日作业: 数组排序方法总结
黄忠倚的博客
Original
804 people have browsed it

实例

<?php
/医院
 * 一、正序:sort(),asort(),ksort()
 * 二、反转:rsort(),arsort(),krsort()
 * reversal,associate()
 */
echo '<h2>常用数组函数(四)</h2><hr color="green">';
$arr = ['id'=>3, 'name'=>'Kevin','course'=>'php','grade'=>60,5=>true];
echo '<pre>';
echo '<p>原始数组</p>';
print_r($arr);
echo '<hr color="red">';

// //1.sort(&$arr,$flag):正序且键名重置
// sort($arr);
// sort($arr,SORT_NUMERIC);
// sort($arr,SORT_STRING); //ASCII
// print_r($arr);


// //2.asort(&$arr,$flag):正序且键名保留
// asort($arr); //忽略类型
// asort($arr,SORT_NUMERIC);
// asort($arr,SORT_STRING); //ASCII
// print_r($arr);

// //3.sort(&$arr,$flag):键名排序
// ksort($arr);
// ksort($arr,SORT_NUMERIC);
// ksort($arr,SORT_STRING); //ASCII
// print_r($arr);

// //4.sort(&$arr,$flag):倒序且键名忽略
// rsort($arr);
// rsort($arr,SORT_NUMERIC);
// rsort($arr,SORT_STRING); //ASCII
// print_r($arr);

// //5.sort(&$arr,$flag):倒序且键名保留
// arsort($arr);
// arsort($arr,SORT_NUMERIC);
// arsort($arr,SORT_STRING); //ASCII
// print_r($arr);

// //6.sort(&$arr,$flag):键名反转排序
// krsort($arr);
// krsort($arr,SORT_NUMERIC);
// krsort($arr,SORT_STRING); //ASCII
// print_r($arr);

//7.usort()
$arr1 = [10, 4, 22, 99, 35, 18];
print_r($arr1);
usort($arr,function($a,$b) {
	$res = $a-$b;
	if ($res == 0) {
		return 0;
	} else if($res >0) {
		return true;
	} else {
		return false;
	}
});
echo '<p>排序后</p>';
print_r($arr1);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post