doubipiao1611
doubipiao1611
2013-04-01 10:57
浏览 10
已采纳

当我在php中知道这个值时,如何获取数组的键[duplicate]

This question already has an answer here:

I have an array like this:

$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');

How can I get key 'age' with value '23'?

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • dongzhiyi2006
    dongzhiyi2006 2013-04-01 10:59
    已采纳

    you are looking for array_keys http://www.php.net/manual/en/function.array-keys.php

    print_r(array_keys($array, "23")); // age
    

    OR by HamZa DzCyberDeV

    echo array_keys($array, "23")[0];
    

    http://codepad.viper-7.com/bZErGT

    点赞 评论
  • dongyudun6465
    dongyudun6465 2013-04-01 10:58

    use array_search()

    $key= array_search($value, $array);
    

    example:

    $array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
    echo array_search("23", $array); //age
    
    点赞 评论
  • douyu1990
    douyu1990 2013-04-01 10:59
    <?php 
        $array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
        echo array_keys($array, "23") .":". $array['age'];
    ?>
    

    Output

    age : 23
    
    点赞 评论
  • dongsutao8921
    dongsutao8921 2013-04-01 11:02

    try this

    <?php
        $arr = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
    
    foreach ($arr as $key => $value) {
        echo "Key: $key; Value: $value<br />
    ";
    }
    ?>
    
    点赞 评论
  • doumi4676
    doumi4676 2013-04-01 11:03

    try this

    $my_arr = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
    
    echo get_array_by_value($my_arr, '23');
    
    function get_array_by_value($my_arr = '', $arr_value = '') {
      $new_arr = array_flip($my_arr);
      if (isset($new_arr[$arr_value])) {
        return $new_arr[$arr_value];
      }
    }
    

    output

    age
    
    点赞 评论
  • doutui2883
    doutui2883 2013-04-01 11:05

    I think you want to find the keys, for values 23, do it like this:

    $array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
    
    while ($value = current($array)) {
        if ($value == '23') {
            echo key($array)."
    ";
        }
        next($array);
    }
    
    点赞 评论
  • douhe3313
    douhe3313 2013-04-01 11:06

    It can be done easily using array_search function.This Function Searches the array for a given value and returns the corresponding key if successful.

    <?php
    $array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
    $key1 = array_search('23', $array); 
    echo '<p>Key 1='.$key1.'</p>';
    $key2 = array_search('Van Pham', $array);
    echo '<p>Key 2='.$key2.'</p>';
    $key3 = array_search('male', $array);
    echo '<p>Key 3='.$key3.'</p>';
    $key4 = array_search('female', $array);
    echo '<p>Key 4='.$key4.'</p>';
    ?>
    

    Output:

    Key 1=age
    
    Key 2=name
    
    Key 3=sex
    
    Key 4=
    

    DEMO

    点赞 评论
  • dp7311
    dp7311 2013-04-01 11:06

    Try with

    foreach($array as $key => $value)
    {
       if($key == 'age' && $value == '23')
           echo $key.'-'.$value;
    }
    
    点赞 评论

相关推荐