From 98cf6179913e1dbc26c3a537a6d0ef2255c025c0 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Mon, 19 Nov 2001 08:56:03 +0000 Subject: [PATCH] - Fix for bug #14104 (added documentation on how to pass object member functions to usort) git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@62786 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/array.xml | 55 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/functions/array.xml b/functions/array.xml index 75a25a559c..d57272f022 100644 --- a/functions/array.xml +++ b/functions/array.xml @@ -1,5 +1,5 @@ - + Array Functions Arrays @@ -3537,6 +3537,10 @@ while (list ($key, $value) = each ($a)) { second. If two members compare as equal, their order in the sorted array is undefined. + + It is also possible to use a member function of an object as the + comparison function. See example number 3 below. + <function>usort</function> example @@ -3622,6 +3626,55 @@ $fruits[2]: lemons + + + + + <function>usort</function> example using a member function of an object + + +class TestObj { + var $name; + + function TestObj($name) + { + $this->name = $name; + } + + /* This is the static comparing function: */ + function cmp_obj($a, $b) + { + $al = strtolower($a->name); + $bl = strtolower($b->name); + if ($al == $bl) return 0; + return ($al > $bl) ? +1 : -1; + } +} + +$a[] = new TestObj("c"); +$a[] = new TestObj("b"); +$a[] = new TestObj("d"); + +uasort($a, array ("TestObj", "cmp_obj")); + +foreach ($a as $item) { + print $item->name."\n"; +} + + + + + This example would display: + + + + +b +c +d + + +