Home  >  Article  >  Backend Development  >  How to use php usort function

How to use php usort function

藏色散人
藏色散人Original
2019-08-27 13:27:212889browse

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);

Parameters

array required. Specifies the array to be sorted.

myfunction Optional. A string that defines a callable comparison function. If the first argument is f2c570bc5a616fb55b90df8c3566974f and the second argument is, the corresponding comparison function must return an integer of f2c570bc5a616fb55b90df8c3566974f 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");
?>

Output:

2
4
6
8

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!

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