Get all departments from Active Directory with PHP and ADLDAP

I am trying to display a simple list of all departments in our organization by querying Active Directory, with PHP, using the ADLDAP Class.  I know how to display information based on users, but not by departments. Alternatively I have tried to display just the departments by running an infoCollection query on all users, but I've been unsuccessful filtering duplicate departments.

Here's the output I'm trying to achieve:

1. Accounting
2. Corporate
3. Human Resources
4. Information Technology
5. Marketing
6. ...

Here is my code so far:

include ('/var/www/html/ldap/adLDAP.php');
        try {
		    $adldap = new adLDAP();
        }
        catch (adLDAPException $e) {
            echo $e; 
            exit();   
        }
	
$usernames = $adldap->user()->all();
$users = array();
$count= 0;
foreach ($usernames as $username)
{

$result=$adldap->user()->infoCollection($username, array("department"));	
$department = $result->department;
if (($department !='') && ($department != $department2))  {
$count++;

echo $count . ". " . $department . "<br>"; 

$department2 = $department;
} 

} 

Open in new window


Thank you
kmgishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What about this code?
include ('/var/www/html/ldap/adLDAP.php');
try {
	$adldap = new adLDAP();
}
catch (adLDAPException $e) {
	echo $e; 
	exit();   
}
	
$usernames = $adldap->user()->all();
$users = array();
$count= 0;
$departments = array();
foreach ($usernames as $username)
{
	$result=$adldap->user()->infoCollection($username, array("department"));	
	$departments[] = $result->department;
} 

echo "Total departments : " . count($departments);
echo "<pre>";
print_r($departments);
echo "</pre>";

Open in new window

0
kmgishAuthor Commented:
Hi JulianH,

Thanks for your response, but unfortunately, your solution is still printing duplicate departments.

Mark
0
Julian HansenCommented:
Sorry - there was a mistake should have been
include ('/var/www/html/ldap/adLDAP.php');
try {
	$adldap = new adLDAP();
}
catch (adLDAPException $e) {
	echo $e; 
	exit();   
}
	
$usernames = $adldap->user()->all();
$users = array();
$count= 0;
$departments = array();
foreach ($usernames as $username)
{
	$result=$adldap->user()->infoCollection($username, array("department"));	
        // This is what enforces the uniqueness
        // keys are unique so there will only be one of each
	$departments[$result->department] = $result->department;
} 

echo "Total departments : " . count($departments);
echo "<pre>";
print_r($departments);
echo "</pre>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

kmgishAuthor Commented:
Yeah, that's pretty close to exactly what I need.  Thanks!
0
kmgishAuthor Commented:
Quick question through: how do I remove the key from the printed results?  I need to use the results in a form select dropdown.

Right now it's returning: [Information Technology] => Information Technology

I need to be able to separate out each department, so I can wrap html tags around them.

Thanks
0
Julian HansenCommented:
Use a foreach loop
<select name="departments">
<?php
foreach($departments as $value) {
  echo <<< OPTION
      <option value="{$value}">{$value}</option>
OPTION;
}
?>
</select>

Open in new window

0
kmgishAuthor Commented:
Perfect!!!  Thank you very much!!!
0
Julian HansenCommented:
You are most welcome - good luck with your project.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.