douxieti6851 2014-03-18 16:06
浏览 32
已采纳

在PHP中读取数组值并分配给单个字符串

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] => john.smith@mydomain.com
    )
    [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:

 john.smith@mydomain.com
  • 写回答

2条回答 默认 最新

  • douling1936 2014-03-18 16:10
    关注

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

    (Array) [0](Array) > [mail](Array) > [0](string) => john.smith@mydomain.com
    
    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决