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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

kmgishAuthor Commented:
Hi JulianH,

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

Mark
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

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

kmgishAuthor Commented:
Yeah, that's pretty close to exactly what I need.  Thanks!
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
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

kmgishAuthor Commented:
Perfect!!!  Thank you very much!!!
Julian HansenCommented:
You are most welcome - good luck with your project.
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.