How to pull entries out of ldap_get_entries array

江枫思渺然 提交于 2020-07-23 06:38:52

问题


I am trying to get a list of users in a Active Directory group using php ldap_get_entries. I am able to connect to the ldap server and bind without a problem. This issue I have is the result I get from the array when using ldap_get_entires. Here is what I am using to get the data from the group:

$result = ldap_search($ldapConn, $ldaptree, "(member=*"),array('member'));
$data = ldap_get_entries($ldapConn, $result);
print_r($data);

What I get is this:

Array([count] => 1 [0] => Array([member] => Array([count => 3 [0] => CN=Mike Jones,CN=Users,DC=DOMAIN,DC=NET [1] => CN=Van Smith,CN=Users,DC=DOMAIN,DC=NET [2] => CN=Jane Doe,CN=Users,DC=DOMAIN,DC=NET) [0] => member[count] => 1 [dn] => CN=Cool Guys,CN=Users,DC=DOMAIN,DC=NET))

How do I pull out just the names from this array to look like this?

Mike Jones 
Van Smith 
Jane Doe

回答1:


Thanks to @Thefourthbird for the suggestion. He suggested I use the following to take out the names I need.

foreach ($array[0]["member"] as $key => $member) {
    if (is_int($key)) {
        $User[] = explode('=', explode(',', $member)[0])[1] . PHP_EOL;
    }
}

The only thing that was left to do was organize the data in alphabetical order. I used a array_multisort function to do this.

array_multisort($User, SORT_ASC);
foreach ($User as $key => $SortUser) {
    echo $SortUser;
}

Now all the names come out on their own line and in alphabetical order.

Jane Doe
Mike Jones 
Van Smith 


来源:https://stackoverflow.com/questions/61671384/how-to-pull-entries-out-of-ldap-get-entries-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!