2002-05-12 08:19:28 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2007-06-20 22:25:43 +00:00
|
|
|
<!-- $Revision: 1.20 $ -->
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.uksort" xmlns="http://docbook.org/ns/docbook">
|
2006-10-31 11:24:02 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>uksort</refname>
|
|
|
|
<refpurpose>Sort an array by keys using a user-defined comparison function</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>bool</type><methodname>uksort</methodname>
|
|
|
|
<methodparam><type>array</type><parameter role="reference">array</parameter></methodparam>
|
|
|
|
<methodparam><type>callback</type><parameter>cmp_function</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>uksort</function> will sort the keys of an array using a
|
|
|
|
user-supplied comparison function. If the array you wish to sort
|
|
|
|
needs to be sorted by some non-trivial criteria, you should use
|
|
|
|
this function.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Function <parameter>cmp_function</parameter> should accept two
|
|
|
|
parameters which will be filled by pairs of <parameter>array</parameter> keys.
|
|
|
|
The comparison function must return an integer less than, equal
|
|
|
|
to, or greater than zero if the first argument is considered to
|
|
|
|
be respectively less than, equal to, or greater than the
|
|
|
|
second.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
&return.success;
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>uksort</function> example</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2006-10-31 11:24:02 +00:00
|
|
|
function cmp($a, $b)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2006-04-05 08:28:14 +00:00
|
|
|
$a = ereg_replace('^(a|an|the) ', '', $a);
|
|
|
|
$b = ereg_replace('^(a|an|the) ', '', $b);
|
|
|
|
return strcasecmp($a, $b);
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2006-04-05 08:28:14 +00:00
|
|
|
$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2003-08-17 12:21:03 +00:00
|
|
|
uksort($a, "cmp");
|
2002-04-15 00:12:54 +00:00
|
|
|
|
2005-07-01 13:07:50 +00:00
|
|
|
foreach ($a as $key => $value) {
|
2002-04-15 00:12:54 +00:00
|
|
|
echo "$key: $value\n";
|
|
|
|
}
|
2003-05-30 18:12:53 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2006-04-05 08:28:14 +00:00
|
|
|
an apple: 3
|
|
|
|
a banana: 4
|
|
|
|
the Earth: 2
|
|
|
|
John: 1
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
See also <function>usort</function>, <function>uasort</function>,
|
|
|
|
<function>sort</function>, <function>asort</function>,
|
|
|
|
<function>arsort</function>, <function>ksort</function>,
|
|
|
|
<function>natsort</function>, and <function>rsort</function>.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- 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
|
|
|
|
-->
|