Join Up!
104886 members and counting!

 
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousuksortAspellnext
Last updated: Tue, 29 Oct 2002
view the printer friendly version or the printer friendly version with notes or change language to Czech | German

usort

(PHP 3>= 3.0.3, PHP 4 )

usort --  Ordena una matriz por valores mediante una funcion definida por el usuario

Descripción

void usort ( array matriz, function func_comparar)

Esta función ordenará una matriz por sus valores utilizando una función suministrada por el usuario. Si la matriz que desea ordenar necesita utilizar un criterio poco trivial, esta es la función que deberá usar.

La función de comparación deberá devolver un entero menor, igual, o mayor que cero, si el primer argumento se considera respectivamente menor que, igual que, o mayor que el segundo. Si dos miembros resultan ser iguales, su orden en la matriz ordenada será cualquiera.

Ejemplo 1. Ejemplo de usort()

function cmp ($a, $b) {   
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
}
$a = array (3, 2, 5, 6, 1);
usort ($a, cmp);
while (list ($clave, $valor) = each ($a)) {
    echo "$clave: $valor\n";
}
Este ejemplo mostrará: 0: 6 1: 5 2: 3 3: 2 4: 1

Nota: Obviamente en este caso trivial la función rsort() habría sido más apropiada.

Aviso

La función quicksort subyacente en ciertas librerías de C (tales como las de Solaris) pueden hacer que el PHP falle si la función de comparación no devuelve valores consistentes.

Vea también: arsort(), asort(), ksort(), rsort() y sort().

User Contributed Notes
usort
add a note about notes
There are no user contributed notes for this page.
previousuksortAspellnext
Last updated: Tue, 29 Oct 2002
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: http://phpbuilder.com/
Last updated: Thu Oct 31 18:34:28 2002 EST