在嵌套关联数组中查找值

时间:2013-10-11 19:58:36

标签: php arrays

我希望使用'SamAccountName'的值获取'GUID'的值。即我只有值's'SamAccountName',我想获得该数组部分的'GUID'值。

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] => johnn@playgroundla.com
        [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] => csliney@playgroundla.com
        [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] => lee@playgroundla.com
        [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] => theresa@playgroundla.com
        [AccountExpires] => 
        [Enabled] => 
        [EnabledFeatures] => Array
            (
                [string] => Array
                    (
                        [0] => ActiveSync
                        [1] => MSExchangeMailboxes
                        [2] => ActiveDirectoryUsers
                    )

            )

        [LastLogonTimestamp] => 
    )
)

这最初是一个stdClass对象,但我使用json_decode(json_encode($ obj),true)转换为关联数组。

5 个答案:

答案 0 :(得分:1)

您可以使用简单的循环来获取它

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

答案 1 :(得分:1)

这就是你要找的东西吗?

function findBySam($arrayList, $sam) {

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

答案 2 :(得分:1)

听起来你想获得'SamAccountName'值的GUID部分。使用foreach循环:

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

$guid = getGUID("SamAccountNameHere", $yourArray);

答案 3 :(得分:0)

以下是您可以使用的功能示例。这假定在数组中只有一个具有SamAccountName的对象(它只使用它找到的第一个对象)。它返回匹配数组的GUID,如果找不到匹配SamAccountName的数组,则返回false。

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

答案 4 :(得分:0)

你可以使用php的array_filter函数: http://php.net/manual/en/function.array-filter.php

示例:

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

不是一个非常优雅的解决方案......但它应该有效。