0

I have a PHP array which gives this result:

Array
(
  [count] => 1
  [0] => Array
  (
    [sn] => Array
    (
      [count] => 1
      [0] => Smith
    )
    [0] => sn
    [givenname] => Array
    (
      [count] => 1
      [0] => John
    )
    [1] => givenname
    [mail] => Array
    (
      [count] => 1
      [0] => [email protected]
    )
    [2] => mail
    [count] => 3
    [dn] => CN=John Smith,OU=IT Admin,DC=mylocaldomain,DC=local
  )
)

How do I go about extracting the Email address from this array result into a single string? I need to able to reference the Email address, but can't find a way to pull that information out.

The meat of the LDAP code is along the lines of:

$dn = “OU=IT Admin,DC=mylocaldomain,DC=local”;
$ds = $ldapconnectionstring;
$person = “johnsmith”;
$filter="(|(sn=$person*)(samaccountname=$person*))";
$justthese = array("ou", "sn", "givenname", "mail");
$sr=ldap_search($ds, $dn, $filter, $justthese);
$info = ldap_get_entries($ds, $sr);

echo '<pre>'; print_r($info); echo '<pre/>';

I've googled this to death, read the PHP documentation about arrays, and read various similar posts here, but nothing seems to work for me.

I need to end up with:

 echo $email;

and get back:

 [email protected]
2
  • 1
    $email = $info[0]['mail'][0]; Commented Mar 18, 2014 at 16:09
  • The dump tells you the EXACT array construct/path you need to follow to get to the email address... Commented Mar 18, 2014 at 16:12

2 Answers 2

1

Basic DUMP analysis 101:

Omitting the irrelevant parts of the dump:

 Array                                            <-- $array
[0] => Array                                      <-- [0]
        [mail] => Array                           <-- ['mail']
                [0] => [email protected]    <-- [0]

giving you

$array[0]['mail'][0];
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks, much appreciated. Sorry stack overflow doesn't allow me to mark both answers as correct! :)
0

All you need to do is read what print_r() shows you.

(Array) [0](Array) > [mail](Array) > [0](string) => [email protected]

$info = ldap_get_entries($ds, $sr);
$email = $info[0]['mail'][0];
echo $email;

See the section Accessing array elements with square bracket syntax on these docs http://docs.php.net/manual/en/language.types.array.php

Comments

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.