2013-01-19 75 views
1

我需要從ldap查詢中轉換字符串。我正在查詢用戶帳戶的Active Directory服務器。這是我拉的字符串。PHP字符串嵌套數組

"CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local" 

我希望它轉換爲一個數組,看起來像這樣

$array['local']['OCtest']['Department & Buildings']['Duck Commander']['Users']['Phil Robertson']=1; 

$array([1]=>'local,[2]=>'OCtest',[3]='Depart',[4]='Duck Commander',[5]='Users'); 

到目前爲止,我有

示例代碼---

$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department &  Buildings,DC=OCSDtest,DC=local",1); 
    unset($dnn2['count']); 
    echo "<pre>"; 
    print_r(array_reverse($dnn2)); 

我需要什麼?

+0

你想要一個6維陣列,或6個元素的數組? – Barmar

+0

6維數組。當我添加其他用戶時,它將被添加到數組中。謝謝 – joegyoung

回答

0
$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department &  Buildings,DC=OCSDtest,DC=local",1); 
unset($dnn2['count']); 
$result = null; 
$ref =& $result; 
foreach (array_reverse($dnn2) as $dn) { 
    $ref = array($dn => null); 
    $ref =& $ref[$dn]; 
} 
print_r($result); 
0

試試這個

$a = array(); 
foreach($dnn2 as $dn) 
{ 
    $arr = explode('=',$dn); 
    $a[] = $arr[1]; //or $a[] = array($arr[1]); for 6 dimensional array 
} 

print_r($a); 
+0

這只是一個數組([1] =「item」,[2] =「item」)。我正在尋找array ['item'] ['item'] ['item']。 – joegyoung

+0

應該是'$ a [] = array($ arr [1]);' – codingbiz

1

試試這個

$arrayvalue = array(); 
    foreach($dnn2 as $dn) 
    { 
     $temp = explode('=',$dn); 
     $temp1 = substr($temp[1], 0, strpos($temp1[1], ',')); 
     $arrayvalue[] = $temp1; 
    } 

    print_r($arrayvalue); 
+0

'ldap_explode_dn()'已經在'='之前移除了部分,你不需要分割它。 – Barmar