小编典典

PHP - 获取数组值的键名

all

我有一个数组如下:

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'我怎样才能做到这一点?


阅读 44

收藏
2022-07-04

共1个答案

小编典典

如果您有一个值并想找到密钥,请array_search()像这样使用:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key现在将包含值的键'a'(即'first')。

2022-07-04