note 41759 deleted from function.ldap-sort by danbrown
| From: | danbrown@php.net | Date: | Wed, 31 Mar 2010 16:23:02 +0000 |
| Subject: | note 41759 deleted from function.ldap-sort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167574@lists.php.net to get a copy of this message | ||
Note Submitter: helmut dot patay at scandio dot de
----
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!