How to use php usort function

藏色散人
Release: 2023-02-23 11:26:01
Original
3018 people have browsed it

How to use php usort function

How to use php usort() function

php usort() function definition and usage

usort() usage User-defined comparison function sorts arrays.

Syntax

usort(array,myfunction);
Copy after login

Parameters

array required. Specifies the array to be sorted.

myfunction Optional. A string that defines a callable comparison function. If the first argument is <, =, > and the second argument is, the corresponding comparison function must return an integer of <, =, > 0.

Example

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");
?>
Copy after login

Output:

2
4
6
8
Copy after login

The above is the detailed content of How to use php usort function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template