简体   繁体   English

查询后如何对ldap结果进行排序

[英]How to sort ldap result after query

i have a code where i want to sort ldap results by sn. 我有一个代码,我想按sn对ldap结果进行排序。 But it's not working. 但这不起作用。 What i am doing wrong? 我做错了什么? Thanks! 谢谢!

$att = array("company","samaccountname","ou","name","displayname", "sn", "givenname", "mail", "telephonenumber", "mobile","title","department");
$result=ldap_search($ldap_connection, $searchdn, $filter, $att);

$info = ldap_get_entries($ldap_connection, $result);

ksort($info['sn']);

for ($i=0; $i<$info["count"]; $i++)....

You can use PHP's client side ldap sort function: 您可以使用PHP的客户端ldap排序功能:

// Search
$results = ldap_search($ldap_connection, $searchdn, $filter, $att);

// Sort -- attribute on which to sort needs to be included in $att array
ldap_sort($ldap_connection, $result, 'sn');

// Get data
$info = ldap_get_entries($ldap_connection, $result);

Use uasort for that like this: 使用uasort这样的:

uasort($info, function($a, $b) {
    return strnatcasecmp($a['sn'], $b['sn']);
}) ;

(not tested but should work) (未经测试,但应该可以工作)

Using ldap_sort is deprecated as the underlying C-Function is deprecated for some years now and the sorting algorithm is also not flexible. 不推荐使用ldap_sort因为不推荐使用底层C函数已经有很多年了,并且排序算法也不灵活。 With this approach you get the same functionality but with much more flexibility. 使用这种方法,您可以获得相同的功能,但灵活性更高。 And as both only sort the returned resultset you are better off using that approach. 而且,由于两者都只对返回的结果集进行排序,因此您最好使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM