php: how to get associative array key from numeric index?

后端 未结 9 2204
臣服心动
臣服心动 2020-12-02 15:21

If I have:

$array = array( \'one\' =>\'value\', \'two\' => \'value2\' );

how do I get the string one back from $ar

9条回答
  •  长情又很酷
    2020-12-02 15:34

    Expanding on Ram Dane's answer, the key function is an alternative way to get the key of the current index of the array. You can create the following function,

        function get_key($array, $index){
          $idx=0;
          while($idx!=$index  && next($array)) $idx++;
          if($idx==$index) return key($array);
          else return '';
        }
    

提交回复
热议问题