mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 17:08:54 +00:00
added multi-dimensional array example to the usort function and added quotes around the user-defined function parameters of the usort and uksort function examples
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@29941 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
2f45f8f85c
commit
2f40451f32
1 changed files with 50 additions and 9 deletions
|
@ -2220,12 +2220,15 @@ fruits[3] = orange
|
|||
<example>
|
||||
<title><function>Uksort</function> example</title>
|
||||
<programlisting role="php">
|
||||
function mycompare ($a, $b) {
|
||||
function cmp ($a, $b) {
|
||||
if ($a == $b) return 0;
|
||||
return ($a > $b) ? -1 : 1;
|
||||
}
|
||||
|
||||
$a = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
|
||||
uksort ($a, mycompare);
|
||||
|
||||
uksort ($a, "cmp");
|
||||
|
||||
while (list ($key, $value) = each ($a)) {
|
||||
echo "$key: $value\n";
|
||||
}
|
||||
|
@ -2291,8 +2294,11 @@ function cmp ($a, $b) {
|
|||
if ($a == $b) return 0;
|
||||
return ($a > $b) ? -1 : 1;
|
||||
}
|
||||
|
||||
$a = array (3, 2, 5, 6, 1);
|
||||
usort ($a, cmp);
|
||||
|
||||
usort ($a, "cmp");
|
||||
|
||||
while (list ($key, $value) = each ($a)) {
|
||||
echo "$key: $value\n";
|
||||
}
|
||||
|
@ -2312,14 +2318,49 @@ while (list ($key, $value) = each ($a)) {
|
|||
4: 1
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
Obviously in this trivial case the <function>rsort</function>
|
||||
function would be more appropriate.
|
||||
</para>
|
||||
</note>
|
||||
<para>
|
||||
<example>
|
||||
<title><function>Usort</function> example using mulit-dimensional array</title>
|
||||
<programlisting role="php">
|
||||
function cmp ($a, $b) {
|
||||
return 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"] . "\n";
|
||||
}
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
When sorting a multi-dimensional array, $a and $b contain references to the first index of the array.
|
||||
</para>
|
||||
<para>
|
||||
This example would display:
|
||||
</para>
|
||||
<para>
|
||||
<informalexample>
|
||||
<programlisting>
|
||||
$fruits[0]: apples
|
||||
$fruits[1]: grapes
|
||||
$fruits[2]: lemons
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
<para>
|
||||
<note>
|
||||
<para>
|
||||
Obviously in this trivial case the <function>rsort</function>
|
||||
function would be more appropriate.
|
||||
</para>
|
||||
</note>
|
||||
<warning>
|
||||
<para>
|
||||
The underlying quicksort function in some C libraries (such as
|
||||
|
|
Loading…
Reference in a new issue