2013-10-11 8 views
1

'SamAccountName'값으로 'GUID'의 값을 가져 오려고합니다. 즉, 값이 pf 'SamAccountName'이고 배열의 해당 부분에 대한 'GUID'값을 얻고 싶습니다. 중첩 된 연관 배열에서 값 찾기

Array 
(
    [0] => Array 
    (
     [DistinguishedName] => [email protected],OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local 
     [GUID] => 26d7c204-7db7-4601-8cd2-0dd0d3b37d97 
     [OriginatingServer] => dcprov024-CA-1.exch024.domain.local 
     [Name] => [email protected] 
     [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] => [email protected],OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local 
     [GUID] => 71224be8-1b8b-46e7-97ef-2cd873bf9b7f 
     [OriginatingServer] => dcprov024-CA-1.exch024.domain.local 
     [Name] => [email protected] 
     [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] => [email protected],OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local 
     [GUID] => b428b57f-4cd4-4243-a76a-f25f5ff3be97 
     [OriginatingServer] => dcprov024-CA-1.exch024.domain.local 
     [Name] => [email protected] 
     [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] => [email protected],OU=playgroundla,OU=Hosting,DC=exch024,DC=domain,DC=local 
     [GUID] => 4b2aee17-9e88-4de9-b95b-63a9877835a6 
     [OriginatingServer] => dcprov024-CA-1.exch024.domain.local 
     [Name] => [email protected] 
     [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] => 
    ) 
) 

원래 stdClass 객체했지만, 난 연관 배열로 변환 (로 json_encode ($의 OBJ), TRUE) json_decode을 사용했다.

+0

이 권리를 얻으려면 : SamName 예. mario 그리고 SamName이 mario라는 것을 알고있는 동일한 배열의 guid를 반환하는 함수가 필요합니까? – Soundz

+0

예, 그게 내가 필요한 것입니다. 그래서 예를 들어 csliney_playgroundla는 71224be8-1b8b-46e7-97ef-2cd873bf9b7f – Dru

답변

1

'SamAccountName'값에 대해 GUID 부분을 가져오고 싶은 소리가납니다.

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

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

고마워. 내가 생각했던 것보다 훨씬 간단했다. – Dru

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 
} 
+0

의'$ yourArray'가 어디에서 오는가? – Soundz

+0

좋은 캐치. 그것은 SO 편집기에서 내 코드를 리팩터링하려고 시도합니다! – Andrew

+0

그래, 우리가 똑같은 생각을 가지고 있었기 때문에, 내 눈을 사로 잡은 느낌이 전혀 없어. – Soundz

1

당신은 그것을

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

를 가져 오기 위해 간단한 루프를 사용할 수있다?

function findBySam($arrayList, $sam) { 

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

당신은 PHP의 기능을 array_filter 사용할 수 있습니다 : http://php.net/manual/en/function.array-filter.php

예 : foreach 루프 사용

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

아니 아주 우아한 해결책을 ... 그러나 그것은 작동합니다.