PHP Usort uses a user-defined comparison function to sort the values in the array

Source: Internet
Author: User
Tags cdata

From:http://www.php100.com/cover/php/2395.html

Usort

(PHP 4, PHP 5)

usort- using a user-defined comparison function to sort values in an array

Description

BOOL usort ( array & $array , callback $cmp _function )

This function will use a user-defined comparison function to sort the values in an array. This function should be used if the array to be sorted needs to be sorted with an unusual standard.

The comparison function must return an integer less than, equal to, or greater than 0, respectively, when the first argument is considered less than, equal to, or greater than the second argument.

Note:

If two members compare the same result, their order in the sorted array is undefined. Before PHP 4.0.6, the user-defined function retains the original order of the cells. However, because of the introduction of a new sorting algorithm in 4.1.0, the result will not be this, because there is no effective solution to this.

Note: This function assigns the new key name to the elements in the array . This will delete the original key name instead of just reordering the key name.

Returns TRUEon success, or FALSEon failure.

Example #1 usort () Example

< Span class= "type" > <?php
function cmp ($a,  $b)
{
    if   ($a  ==  $b)  {
        return 0;
    }
    return  ($a  <  $b)  ? -1 :  1;
}

$a  = array (3, 2, 5, 6, 1);

Usort ($a,  "CMP");

foreach  ($a  as  $key  =>  $value)  {
    echo   "$key:  $value  ";
}
?

The above routines will output:

0:11:22:33:54:6

Note:

It is obvious that the sort () function is more appropriate in this small example.

Example #2 using the usort () example of a multidimensional array

< Span class= "type" > < Span class= "example-contents" > <?php
function cmp ($a,  $b)
{
&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP ($a ["Fruit"],  $b ["fruit"]);
}

$fruits [0]["Fruit"] =  "lemons";
$fruits [1]["Fruit"] =  "apples";
$fruits [2]["fruit"] =  "grapes";

Usort ($fruits,  "CMP");

while  (List ($key,  $value)  = each ($fruits))  {
    echo   "$fruits [$key]: "  .  $value ["Fruit"] .  "";
}
?

When a multidimensional array is sorted, $a and $b contains a reference to the first index of the array.

The above routines will output:

$fruits [0]: apples$fruits[1]: grapes$fruits[2]: Lemons

Example #3 usort () example using the object's member function

<?php
Class Testobj {
var $name;

function Testobj ($name)
{
$this->name = $name;
}

/* This is the static comparing function: */
function cmp_obj ($a, $b)
{
$al = Strtolower ($a->name);
$BL = Strtolower ($b->name);
if ($al = = $BL) {
return 0;
}
Return ($al > $bl)? +1:-1;
}
}

$a [] = new Testobj ("C");
$a [] = new Testobj ("B");
$a [] = new Testobj ("D");

Usort ($a, Array ("Testobj", "cmp_obj"));

foreach ($a as $item) {
Echo $item->name. " ";
}
?>

The above routines will output:

Bcd

See Uasort (),uksort (),sort (),asort (),arsort (),ksort (),Natsort (), and Rsort ().

------------------------------------------

If the static function of the class is used as a custom sort function , the notation is:usort ($arr, Array (' self ', ' staticfunction '));

PHP Usort uses a user-defined comparison function to sort the values in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.