PHP字符串到嵌套数组

时间:2013-01-19 05:12:31

标签: php arrays multidimensional-array

我需要从ldap查询转换字符串。我正在查询我的Active Directory服务器以获取用户帐户。这是我拉的字符串。

"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));

我需要什么?

3 个答案:

答案 0 :(得分:1)

试试这个

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

    print_r($arrayvalue);

答案 1 :(得分: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);

答案 2 :(得分: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);