php usort()函数的使用方法
php usort()函数定义和用法
usort() 使用用户自定义的比较函数对数组进行排序。
语法
usort(array,myfunction);
参数
立即学习“PHP免费学习笔记(深入)”;
array 必需。规定要排序的数组。
myfunction 可选。一个定义了可调用比较函数的字符串。如果第一个参数 第二个参数,相应地比较函数必须返回一个 0 的整数。
实例
使用用户自定义的比较函数对数组 $a 中的元素进行排序:Sort the elements of the $a array using a user-defined comparison function:
<?php function my_sort($a,$b) { if ($a==$b) return 0; return ($a<$b)?-1:1; } $a=array(4,2,8,6); usort($a,"my_sort"); ?>
输出:
2 4 6 8
以上就是php usort函数的使用方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号