1

I want to get the value of 'GUID' with the value of 'SamAccountName'. i.e. I only have the value pf 'SamAccountName' and I would like to get the value of 'GUID' for that part of the array.

Array
(
  [0] => Array
    (
        [DistinguishedName] => CN=johnn@playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
        [GUID] => 26d7c204-7db7-4601-8cd2-0dd0d3b37d97
        [OriginatingServer] => dcprov024-CA-1.exch024.domain.local
        [Name] => johnn@playgroundla
        [HostingObjectType] => Array
            (
                [HostingObjectTypes] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchange2007Mailbox
                        [2] => ActiveDirectoryUser
                    )

            )

        [HostingOwners] => Array
            (
                [HostingObjectOwners] => Array
                    (
                        [0] => MSExchange2007Mailboxes
                        [1] => ActiveDirectoryUsers
                    )

            )

        [Attributes] => Array
            (
                [Hidden] => 
                [ReadOnly] => 
                [SpecialAccess] => 
                [Items] => Array
                    (
                    )

            )

        [DisplayName] => John Nolan
        [SamAccountName] => johnn_playgroundla
        [FullSamAccountName] => EXCH024\johnn_playgroundla
        [UserPrincipalName] => [email protected]
        [AccountExpires] => 
        [Enabled] => 
        [EnabledFeatures] => Array
            (
                [string] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchangeMailboxes
                        [2] => ActiveDirectoryUsers
                    )

            )

        [LastLogonTimestamp] => 
    )

[1] => Array
    (
        [DistinguishedName] => CN=csliney@playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
        [GUID] => 71224be8-1b8b-46e7-97ef-2cd873bf9b7f
        [OriginatingServer] => dcprov024-CA-1.exch024.domain.local
        [Name] => csliney@playgroundla
        [HostingObjectType] => Array
            (
                [HostingObjectTypes] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchange2007Mailbox
                        [2] => ActiveDirectoryUser
                    )

            )

        [HostingOwners] => Array
            (
                [HostingObjectOwners] => Array
                    (
                        [0] => MSExchange2007Mailboxes
                        [1] => ActiveDirectoryUsers
                    )

            )

        [Attributes] => Array
            (
                [Hidden] => 
                [ReadOnly] => 
                [SpecialAccess] => 
                [Items] => Array
                    (
                    )

            )

        [DisplayName] => Christopher Sliney
        [SamAccountName] => csliney_playgroundla
        [FullSamAccountName] => EXCH024\csliney_playgroundla
        [UserPrincipalName] => [email protected]
        [AccountExpires] => 
        [Enabled] => 
        [EnabledFeatures] => Array
            (
                [string] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchangeMailboxes
                        [2] => ActiveDirectoryUsers
                    )

            )

        [LastLogonTimestamp] => 
    )

[2] => Array
    (
        [DistinguishedName] => CN=lee@playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
        [GUID] => b428b57f-4cd4-4243-a76a-f25f5ff3be97
        [OriginatingServer] => dcprov024-CA-1.exch024.domain.local
        [Name] => lee@playgroundla
        [HostingObjectType] => Array
            (
                [HostingObjectTypes] => Array
                    (
                        [0] => MSExchange2007Mailbox
                        [1] => ActiveDirectoryUser
                    )

            )

        [HostingOwners] => Array
            (
                [HostingObjectOwners] => Array
                    (
                        [0] => MSExchange2007Mailboxes
                        [1] => ActiveDirectoryUsers
                    )

            )

        [Attributes] => Array
            (
                [Hidden] => 
                [ReadOnly] => 
                [SpecialAccess] => 
                [Items] => Array
                    (
                    )

            )

        [DisplayName] => Lee Roderick
        [SamAccountName] => lee_playgroundla
        [FullSamAccountName] => EXCH024\lee_playgroundla
        [UserPrincipalName] => [email protected]
        [AccountExpires] => 
        [Enabled] => 
        [EnabledFeatures] => Array
            (
                [string] => Array
                    (
                        [0] => MSExchangeMailboxes
                        [1] => ActiveDirectoryUsers
                    )

            )

        [LastLogonTimestamp] => 
    )

[3] => Array
    (
        [DistinguishedName] => CN=theresa@playgroundla,OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local
        [GUID] => 4b2aee17-9e88-4de9-b95b-63a9877835a6
        [OriginatingServer] => dcprov024-CA-1.exch024.domain.local
        [Name] => theresa@playgroundla
        [HostingObjectType] => Array
            (
                [HostingObjectTypes] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchange2007Mailbox
                        [2] => ActiveDirectoryUser
                    )

            )

        [HostingOwners] => Array
            (
                [HostingObjectOwners] => Array
                    (
                        [0] => MSExchange2007Mailboxes
                        [1] => ActiveDirectoryUsers
                    )

            )

        [Attributes] => Array
            (
                [Hidden] => 
                [ReadOnly] => 
                [SpecialAccess] => 
                [Items] => Array
                    (
                    )

            )

        [DisplayName] => Theresa Baker
        [SamAccountName] => theresa_playgroundla
        [FullSamAccountName] => EXCH024\theresa_playgroundla
        [UserPrincipalName] => [email protected]
        [AccountExpires] => 
        [Enabled] => 
        [EnabledFeatures] => Array
            (
                [string] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchangeMailboxes
                        [2] => ActiveDirectoryUsers
                    )

            )

        [LastLogonTimestamp] => 
    )
)

This was originally a stdClass object but I used json_decode(json_encode($obj), true) to convert to an associative array.

2
  • just to get this right: you have the SamName e.g. mario and you want a function which returns the guid of the same array you know the SamName is mario? Commented Oct 11, 2013 at 20:04
  • Yes, that's what I need. So for example csliney_playgroundla should return 71224be8-1b8b-46e7-97ef-2cd873bf9b7f Commented Oct 11, 2013 at 20:07

5 Answers 5

1

Sounds like you want to get the GUID portion for the value of 'SamAccountName'. Use a foreach loop:

function getGUID($san, $array) {
    foreach($array as $a) {
        if($a['SamAccountName'] == $san) {
            return $a['GUID'];
        }
    }
    return false;
}

$guid = getGUID("SamAccountNameHere", $yourArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Was much simpler than I thought.
1

You can use a simple loop to fetch it

$id = 0;
foreach($data as $item) {
  if (isset($item['SamAccountName']) && 'accountName' == $item['SamAccountName']) {
    $id = $item['GUID'];
    break;
  }
}
var_dump($id);

Comments

1

is this what you are looking for?

function findBySam($arrayList, $sam) {

    foreach($arrayList as $array) {
      if($array['SamAccountName'] == $sam) {
        return $array;
      }
    }
    return false;
}

Comments

0

Here is an example of a function that you could use. This assumes that there will be only one object with the SamAccountName that you supply in the array (it just uses the first one that it finds). It returns the GUID of the matching array and false if it cannot find an array with a matching SamAccountName.

function getGuidForSamAccountName($arr, $name) { 
    foreach ($arr as $elem) {
        if ($elem['SamAccountName'] === $name) {
            return $elem['GUID'];
        }
    }
    return false; //No match found
}

3 Comments

where comes $yourArray from?
good catch. that'll teach me to try to refactor my code in the SO editor!
yeah, no hard feeling, just caught my eye since we had the same idea
0

You can use array_filter function of php: http://php.net/manual/en/function.array-filter.php

example:

$GUID = "sample";
array_filter($array, "findElement");
function findElement($el) {
return $el["GUID"] == $_GLOBAL["GUID"];
}

Not a very elegant solution... but it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.