mirror of
https://github.com/php/doc-es.git
synced 2026-03-27 08:52:15 +01:00
- Nuevo documento del capitulo de seguridad (magicquotes.xml) git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@181350 c90b9560-bf6c-de11-be94-00142212c4b1
214 lines
5.0 KiB
XML
214 lines
5.0 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision: 1.4 $ -->
|
|
<!-- EN-Revision: 1.21 Maintainer: lboshell Status: ready -->
|
|
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
|
|
<refentry id="function.usort">
|
|
<refnamediv>
|
|
<refname>usort</refname>
|
|
<refpurpose>
|
|
Ordena una matriz por sus valores usando una función de
|
|
comparación definida por el usuario
|
|
</refpurpose>
|
|
</refnamediv>
|
|
<refsect1>
|
|
<title>Descripción</title>
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>usort</methodname>
|
|
<methodparam><type>array</type><parameter role="reference">matriz</parameter></methodparam>
|
|
<methodparam><type>callback</type><parameter>funcion_comp</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Esta función ordenará una matriz por sus valores
|
|
usando una función de comparación definida por el
|
|
usuario. Si la matriz que desea ordenar necesita ser ordenada
|
|
mediante ciertos criterios especiales, es buena idea usar esta
|
|
función.
|
|
</para>
|
|
<para>
|
|
La función de comparación debe devolver un entero
|
|
menor que, igual, o mayor que cero si el primer argumento es
|
|
considerado menor, igual, o mayor que el segundo,
|
|
respectivamente.
|
|
</para>
|
|
<para>
|
|
<note>
|
|
<para>
|
|
Si dos miembros son comparados como iguales, su orden en la
|
|
matriz resultante es indefinido. Hasta PHP 4.0.6, las funciones
|
|
definidas por el usuario mantenían el orden original
|
|
para esos elementos, pero con el nuevo algoritmo de
|
|
ordenamiento introducido en 4.1.0 este ya no es el caso, ya que
|
|
no hay forma de hacerlo de manera eficiente.
|
|
</para>
|
|
</note>
|
|
</para>
|
|
<para>
|
|
&return.success;
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Ejemplo de <function>usort</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?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");
|
|
|
|
while (list($clave, $valor) = each($a)) {
|
|
echo "$clave: $valor\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
0: 1
|
|
1: 2
|
|
2: 3
|
|
3: 5
|
|
4: 6
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Obviamente en este ejemplo trivial, la función
|
|
<function>sort</function> sería más apropiada.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Ejemplo de <function>usort</function> usando una matriz
|
|
multi-dimensional
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
function cmp($a, $b)
|
|
{
|
|
return strcmp($a["fruta"], $b["fruta"]);
|
|
}
|
|
|
|
$frutas[0]["fruta"] = "limones";
|
|
$frutas[1]["fruta"] = "bananos";
|
|
$frutas[2]["fruta"] = "granadillas";
|
|
|
|
usort($frutas, "cmp");
|
|
|
|
while (list($clave, $valor) = each($frutas)) {
|
|
echo "\$frutas[$clave]: " . $valor["fruta"] . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Cuando se ordena una matriz multi-dimensional,
|
|
<varname>$a</varname> y <varname>$b</varname> contienen
|
|
referencias al primer índice de la matriz.
|
|
</para>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
$frutas[0]: bananos
|
|
$frutas[1]: granadillas
|
|
$frutas[2]: limones
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Ejemplo de <function>usort</function> usando una función
|
|
miembro de un objeto
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class ObjPrueba {
|
|
var $nombre;
|
|
|
|
function ObjPrueba($nombre)
|
|
{
|
|
$this->nombre = $nombre;
|
|
}
|
|
|
|
/* Esta es la funcion estatica de comparacion: */
|
|
function cmp_obj($a, $b)
|
|
{
|
|
$al = strtolower($a->nombre);
|
|
$bl = strtolower($b->nombre);
|
|
if ($al == $bl) {
|
|
return 0;
|
|
}
|
|
return ($al > $bl) ? +1 : -1;
|
|
}
|
|
}
|
|
|
|
$a[] = new ObjPrueba("c");
|
|
$a[] = new ObjPrueba("b");
|
|
$a[] = new ObjPrueba("d");
|
|
|
|
usort($a, array("ObjPrueba", "cmp_obj"));
|
|
|
|
foreach ($a as $item) {
|
|
echo $item->nombre . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
b
|
|
c
|
|
d
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
Vea también <function>uasort</function>,
|
|
<function>uksort</function>, <function>sort</function>,
|
|
<function>asort</function>,
|
|
<function>arsort</function>,<function>ksort</function>,
|
|
<function>natsort</function>, and <function>rsort</function>.
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|