Date: 08/09/00
- Next message: Hojtsy Gabor: "[PHP-DOC] cvs: phpdoc /hu/APPENDICES debugger.xml escaping.xml history.xml http-stuff.xml migration.xml phpdevel.xml regexp.xml"
- Previous message: Daniel Beckham: "[PHP-DOC] cvs: phpdoc /en/functions strings.xml"
- Next in thread: James Moore: "[PHP-DOC] cvs: phpdoc /en/functions array.xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
danbeck Wed Aug 9 07:16:35 2000 EDT
Modified files:
/phpdoc/en/functions array.xml
Log:
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
Index: phpdoc/en/functions/array.xml
diff -u phpdoc/en/functions/array.xml:1.20 phpdoc/en/functions/array.xml:1.21
--- phpdoc/en/functions/array.xml:1.20 Tue Aug 8 15:58:48 2000
+++ phpdoc/en/functions/array.xml Wed Aug 9 07:16:35 2000
@@ -2220,12 +2220,15 @@
<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 @@
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";
}
@@ -2313,13 +2319,48 @@
</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
- Next message: Hojtsy Gabor: "[PHP-DOC] cvs: phpdoc /hu/APPENDICES debugger.xml escaping.xml history.xml http-stuff.xml migration.xml phpdevel.xml regexp.xml"
- Previous message: Daniel Beckham: "[PHP-DOC] cvs: phpdoc /en/functions strings.xml"
- Next in thread: James Moore: "[PHP-DOC] cvs: phpdoc /en/functions array.xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

