duanbipu7601 2011-05-26 21:23
浏览 33

如果我只有值,我如何获得数组元素的键?

How do i get the key of an array element if i only have the value?

So i have this array and i need the key of an email address with the value

$users_emails = array(
 'Spence' => 'spence@someplace.com', 
  'Matt'   => 'matt@someplace.com', 
  'Marc'   => 'marc@someplace.com', 
  'Adam'   => 'adam@someplace.com', 
  'Paul'   => 'paul@someplace.com');

How do i get the 'Adam' if all i have is the value 'adam@someplace.com'.. so basically how do i get the key if i have the value

  • 写回答

3条回答 默认 最新

  • dongshao1981 2011-05-26 21:26
    关注

    Use array_search and this question.

    Example from docs:

    <?php
    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
    
    $key = array_search('green', $array); // $key = 2;
    $key = array_search('red', $array);   // $key = 1;
    ?>
    

    PHP has wonderful documentation and it's quite easy to find the basic stuff with Google.

    <?php
    $key = array_search('matt@someplace.com', $users_emails); // $key = 'Spence';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan