note 81447 deleted from function.ldap-sort by danbrown

From: Date: Wed, 31 Mar 2010 16:20:12 +0000
Subject: note 81447 deleted from function.ldap-sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167568@lists.php.net to get a copy of this message
Note Submitter: headley at my-mail dot ch ---- I've got extend the functionality of the last code. Now you can sort the ldap-array with multiple sort-directions. <?php /*Scoped global*/ $LDAPSortOrder; /*$array is the return from ldap_get_entries $sortBy is an array of values to sort by in the order in which to sort them Example: $sortBy=array('name' => 'ASC','givenname' => 'DESC') ; */ function LDAPSort(&$array,$sortBy){ global $LDAPSortOrder; $LDAPSortOrder = $sortBy; usort($array,'ldapcompare'); } /* The compare functions always check case insensitive*/ function ldapcompare($x,$y){ global $LDAPSortOrder; /* Loops through the items in order until a difference is found */ foreach($LDAPSortOrder as $ele => $direction){ if(strtolower($x[$ele][0]) == strtolower($y[$ele][0])) continue; else{ if(strtolower($x[$ele][0]) < strtolower($y[$ele][0])){ switch(strtoupper($direction)){ case 'ASC': return -1; break; case 'DESC': return 1; break; } } else{ switch(strtoupper($direction)){ case 'ASC': return 1; break; case 'DESC': return -1; break; } } } } return 0; } ?>

« previous php.notes (#167568) next »