开发者社区 问答 正文

PHP-获取数组值的键名

我有如下数组:

function example() { /* some stuff here that pushes items with dynamically created key strings into an array */

return array( // now lets pretend it returns the created array
    'firstStringName' => $whatEver,
    'secondStringName' => $somethingElse
);

}

$arr = example();

// now I know that $arr contains $arr['firstStringName']; 我需要找出的索引,$arr['firstStringName']以便能够遍历array_keys($arr)并'firstStringName'通过其索引返回键字符串。我怎样才能做到这一点?

展开
收起
保持可爱mmm 2020-02-07 00:03:20 332 分享 版权
1 条回答
写回答
取消 提交回答
  • 如果您有一个值并想找到该键,请按以下方式使用array_search():

    $arr = array ('first' => 'a', 'second' => 'b', ); $key = array_search ('a', $arr); $key现在将包含值的键'a'(即'first')。

    问题来源于stack overflow

    2020-02-07 00:03:51
    赞同 展开评论
问答分类:
问答地址: