Hi,
A newbie question. How does one sort multi-dimensional array output from ldap_get_entries? I'm trying to sort the returned attributes. Code snippets:

$myds = ldap_server_connect ($ldap_server_address, $ldap_base_dn, $ldap_root_dn, $ldap_root_pw, true, $myldapconnect, $myldapbind, $myldapentry);
if ($myds == "") {
$justthese = array("uid", "cn", "mail");
$searchstr = "cn=*";
$sr=ldap_search($myldapconnect, $ldap_base_dn, $searchstr, $justthese);

$info = ldap_get_entries($myldapconnect, $sr);
}

How do I sort, say by the "mail" attribute?

Many thanks
cakoay

    give us an example of the array it returns.

      Hi, thanks for the response. Due to lack of PHP knowledge on my part, I output the returned array in the following manner:

        echo "<TABLE>";
        echo "<TR><TD>UID</TD><TD>Common Name</TD><TD>Email</TD></TR>";
        for ($i=0; $i<$info["count"]; $i++) {
           echo "<TR><TD>". $info[$i]["uid"][0] ."</TD>" ;
           echo "<TD>". $info[$i]["cn"][0] ."</TD>";
           echo "<TD>". $info[$i]["mail"][0] ."</TD></TR>";
        }
         echo "</TABLE>";

      Thanks

        more specifically, i mean the output from print_r($info);

          Do you need all entries?

          i just query the entries, i need 😉

              if ($actionldap=='search'){
                  ECHO $MSG_MSG_WAIT;
                  $ds=ldap_connect($LDAP_SERVER);
                  if (!$UserStyle) {include "./include/case_style.inc";}
                      else include "./include/case_style_$UserStyle.inc";
                  ?>
                  <TABLE class=border align=center border=0 cellspacing=1 cellpadding=3 width=85%>
                  <tr><td class=back align=center>
                      <FORM name='ldap2' action='<?=$PHP_SELF?>' method='post'>
                      <input type=hidden name=action value='new'>
                      Bitte w&auml;hlen: <select size=1 name="selemail">
                      <?
                      if ($ds){
                      $r=ldap_bind($ds);
                      $sr=ldap_search($ds,"o=XXXXXX-XXXXX.de", "sn=".$searchpattern."*");
                      $info = ldap_get_entries($ds, $sr);
                      for ($i=0; $i<$info["count"]; $i++) {
                          ?>
                          <option value=<?=$info[$i]["mail"][0]?>><?=$info[$i]["cn"][0]?> < <?=$info[$i]["mail"][0]?> > </option>
                          <?
                      }
                      ldap_close($ds);
                      ?>
                      </select>&nbsp;<input type='submit' value='weiter'>&nbsp;<a href='<?=$PHP_SELF?>'> neue Suche</a>
                      </FORM>
                  </td></tr>
                  </table></br>
                  <?
                  }
                  else
                  {echo "<h4>Verbindung zu LDAP Server nicht mglich</h4>";}
              }
          

          They are not sorted - but filtered.

          You want it sorted? You have to sort the array... don't ask me, how 🙁 My solution just works fine.

            Apologies. Output from print_r($info);:

            Array
            (
            [count] => 4
            [0] => Array
            (
            [cn] => Array
            (
            [count] => 1
            [0] => Administrator
            )

                    [0] => cn
                    [mail] => Array
                        (
                            [count] => 1
                            [0] => [email]admin@domain.com[/email]
                        )
            
                    [1] => mail
                    [uid] => Array
                        (
                            [count] => 1
                            [0] => admin
                        )
            
                    [2] => uid
                    [count] => 3
                    [dn] => cn=Administrator,dc=domain,dc=com
                )
            
            [1] => Array
                (
                    [cn] => Array
                        (
                            [count] => 1
                            [0] => User One
                        )
            
                    [0] => cn
                    [mail] => Array
                        (
                            [count] => 1
                            [0] => [email]userone@domain.com[/email]
                        )
            
                    [1] => mail
                    [uid] => Array
                        (
                            [count] => 1
                            [0] => userone
                        )
            
                    [2] => uid
                    [count] => 3
                    [dn] => cn=User One,dc=domain,dc=com
                )
            
            [2] => Array
                (
                    [cn] => Array
                        (
                            [count] => 1
                            [0] => User Two
                        )
            
                    [0] => cn
                    [uid] => Array
                        (
                            [count] => 1
                            [0] => usertwo
                        )
            
                    [1] => uid
                    [mail] => Array
                        (
                            [count] => 1
                            [0] => [email]usertwo@domain.com[/email]
                        )
            
                    [2] => mail
                    [count] => 3
                    [dn] => cn=User Two,dc=domain,dc=com
                )
            
            [3] => Array
                (
                    [cn] => Array
                        (
                            [count] => 1
                            [0] => YYY
                        )
            
                    [0] => cn
                    [uid] => Array
                        (
                            [count] => 1
                            [0] => YYY
                        )
            
                    [1] => uid
                    [mail] => Array
                        (
                            [count] => 1
                            [0] => [email]yyy@domain.com[/email]
                        )
            
                    [2] => mail
                    [count] => 3
                    [dn] => cn=YYY YYY,dc=domain,dc=com
                )

            )

              Hi, anyone (jmcneese?) can help? Posted array output as displayed above.

                Whenever you're wanting to sort something in some nontrvial fashion, the usort() function is often the way to go.

                To use it, you create a function that, when given two elements of the array, will say whether the first is "before" or "after" the other - for whatever meaning of "before" and "after" you specify.

                function compare_entries($a,$b)
                {
                /* $a and $b are two entries from the array being sorted.
                  If $a is "after" than $b, return -1;
                  if $a is "before" than $b, return 1;
                  If $a and $b are judged to be the same, return 0.
                */
                }
                

                For example, if $array is a list of arrays with keys 'foo', 'bar', 'baz' and 'wibble', and I want to sort them according to 'baz' and then 'wibble' respectively:

                function compare_entries($a,$b)
                {	if($a['baz']>$b['baz'])
                		return -1;
                	if($a['baz']<$b['baz'])
                		return 1;
                	// $a and $b have the same 'baz' value.
                	// Sort among these by 'wibble'.
                	if($a['wibble']>$b['wibble'])
                		return -1;
                	if($a['wibble']<$b['wibble'])
                		return 1;
                	// As far as I'm concerned, $a and $b are equivalent.
                	return 0;
                }
                

                Using this comparison function is simplicity itself. To sort $array using this function I go

                usort($array,'compare_entries');
                

                I hope that's of some help; once you've figured out exactly what it would mean for your array to be sorted, you should be able to encapsulate it in a comparison function and use it in usort(). For further detail, look up usort() in the manual.

                  Many thanks Weedpacket. Will try it out. 🙂

                    Came here to figure out exactly what you (weedpacket) posted.

                    Thanks!🙂

                      Write a Reply...