note 74034 added to function.ldap-sort

From: Date: Wed, 21 Mar 2007 15:29:01 +0000
Subject: note 74034 added to function.ldap-sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-124676@lists.php.net to get a copy of this message
Ben @ xsusio dot net's code below was very useful to me because although I'm not sure why, ldap_sort against AD on the 'mail' attribute (or any other) seems to split the results into 2 sections, so with a result set of around 230, I'd have about 180 (ish) email addresses sorted alphabetically, but then another 50 (or so) records under those, but also sorted alphabetically. I never did work out why that happened (it wasn't to do with multiple values in the fields though, or preceeding spaces in the email address) so Ben's solution meant I didn't have to bother fixing it. However, one drawback was that if a record didn't have an email address (not all our users do), then Ben's sort would throw a warning (Notice: Undefined index: mail). This could also adversely affect the sort so I added an if isset to deal with that instance. Here's the modified code. Thanks Ben. <?php /** * @param array $entries * @param array $attribs * @desc Sort LDAP result entries by multiple attributes. */ function ldap_multi_sort(&$entries, $attribs){ for ($i=1; $i<$entries['count']; $i++){ $index = $entries[$i]; $j=$i; do { // create comparison variables from attributes: $a = $b = null; foreach($attribs as $attrib){ // only do it though if that attribute exists in both records if(isset($entries[$j-1][$attrib]) && isset($index[$attrib])) { $a .= $entries[$j-1][$attrib][0]; $b .= $index[$attrib][0]; } } // do the comparison if ($a > $b){ $is_greater = true; $entries[$j] = $entries[$j-1]; $j = $j-1; }else{ $is_greater = false; } } while ($j>0 && $is_greater); $entries[$j] = $index; } return $entries; } ?> ---- Server IP: 194.145.210.4 Probable Submitter: 80.169.172.95 ---- Manual Page -- http://www.php.net/manual/en/function.ldap-sort.php Edit -- https://master.php.net/note/edit/74034 Del: integrated -- https://master.php.net/note/delete/74034/integrated Del: useless -- https://master.php.net/note/delete/74034/useless Del: bad code -- https://master.php.net/note/delete/74034/bad+code Del: spam -- https://master.php.net/note/delete/74034/spam Del: non-english -- https://master.php.net/note/delete/74034/non-english Del: in docs -- https://master.php.net/note/delete/74034/in+docs Del: other reasons-- https://master.php.net/note/delete/74034 Reject -- https://master.php.net/note/reject/74034 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124676) next »