note 41759 added to function.ldap-sort
| From: | helmut dot patay at scandio dot de | Date: | Wed, 21 Apr 2004 19:02:21 +0000 |
| Subject: | note 41759 added to function.ldap-sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68471@lists.php.net to get a copy of this message | ||
If you want to sort the search results based on any attribute reverse,
please do not use the examples above containing two loops!
You will end up in changing php.ini with increasing the value
of 'max_execution_time' dramatically. No one will use your website
again!
I have used the following construct and it works very well:
// write this function
function myReverseCnCmp( $a, $b ) {
// Note: $b comes before $a because of reverse ordering
return strcasecmp( $b[cn][0], $a[cn][0] );
}
// search
$sr = ldap_search( $ds, $baseDn, $filter );
// before reading the entries, you must sort normally
// in my example I use the attribute 'cn' for sorting
// instead of cn you can use any other attribute
ldap_sort( $ds, $sr, "cn" )
// read the entries
$entries = ldap_get_entries( $ds, $sr );
// now sort the array reverse with usort
usort( $entries, "myReverseCnCmp" );
// now $entries is sorted reverse based on "cn"
// This runs at least 5 times faster than the double loop
// solutions
// Note: If you want to sort reverse based on dynamic
// attribute names, you must do some if's or switch'es,
// but it's really fast!
----
Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41759
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41759&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41759&report=yes
Search -- http://master.php.net/manage/user-notes.php