1

I need to convert a string from an ldap query. I am querying my Active Directory server for user accounts. This is the string that I pulled.

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

I would want it converted to an array that looks like this

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

NOT

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

So far I have

Example code ---

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

What am I needing?

2
  • You want a 6-dimensional array, or an array with 6 elements? Commented Jan 19, 2013 at 5:55
  • 6-dimensional array. When I add other users, it would be added to the array. thanks Commented Jan 19, 2013 at 6:10

3 Answers 3

1

Try this

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

    print_r($arrayvalue);
Sign up to request clarification or add additional context in comments.

1 Comment

ldap_explode_dn() already removes the part before =, you don't need to split it.
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);

Comments

0

Try this

$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 Comments

This just makes a array( [1]="item",[2]="item"). I am looking for array['item']['item']['item'].
Should be $a[] = array($arr[1]);

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.