如何获取数组中特定值的键名

发布于 2024-12-24 23:59:10 字数 557 浏览 3 评论 0原文

我有一个如下数组:

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

I have an array as the following:

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'];

I need to find out the index of $arr['firstStringName'] so that I am able to loop through array_keys($arr) and return the key string 'firstStringName' by its index. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

榆西 2024-12-31 23:59:10

array_search() 是以下功能:实现这个:

array_search — 在数组中搜索给定值,如果成功则返回第一个对应的键

如果您有一个值并想要查找该键,请像这样使用 array_search()

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

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

array_search() is the feature that accomplishes this:

array_search — Searches the array for a given value and returns the first corresponding key if successful

If you have a value and want to find the key, use array_search() like this:

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

$key will now contain the key for the value 'a' (that is, 'first').

因为看清所以看轻 2024-12-31 23:59:10
key($arr);

将返回当前数组元素的键值

http://uk.php.net/manual/en/ function.key.php

key($arr);

will return the key value for the current array element

http://uk.php.net/manual/en/function.key.php

浅笑轻吟梦一曲 2024-12-31 23:59:10

如果我理解正确,您可以使用:

foreach($arr as $key=>$value)
{
  echo $key;
}

参见 PHP 手册

If I understand correctly, you can use:

foreach($arr as $key=>$value)
{
  echo $key;
}

See PHP manual

神也荒唐 2024-12-31 23:59:10

这是另一个选择

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 

Here is another option

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 
<逆流佳人身旁 2024-12-31 23:59:10

如果名称是动态的,那么您必须有类似的内容

$arr[$key]

,这意味着 $key 包含该键的值。

您可以使用 array_keys() 来获取数组的所有键,例如

$arr = array('a' => 'b', 'c' => 'd')
$x = array_keys($arr);

会给您

$x = array(0 => 'a', 1 => 'c');

If the name's dynamic, then you must have something like

$arr[$key]

which'd mean that $key contains the value of the key.

You can use array_keys() to get ALL the keys of an array, e.g.

$arr = array('a' => 'b', 'c' => 'd')
$x = array_keys($arr);

would give you

$x = array(0 => 'a', 1 => 'c');
寄意 2024-12-31 23:59:10

您可以这样循环遍历数组的键:

foreach($arr as $key=>$value)
{

}

You can loop through the keys of the array this way:

foreach($arr as $key=>$value)
{

}
偏闹i 2024-12-31 23:59:10

如果需要返回具有相同值的数组元素,请使用 array_keys() 函数

$array = array('red' => 1, 'blue' => 1, 'green' => 2);
print_r(array_keys($array, 1));

if you need to return an array elements with same value, use array_keys() function

$array = array('red' => 1, 'blue' => 1, 'green' => 2);
print_r(array_keys($array, 1));
遮了一弯 2024-12-31 23:59:10

使用 array_keys() 获取所有唯一键的数组。

请注意,具有命名键(例如 $arr)的数组也可以通过数字索引(例如 $arr[0])进行访问。

http://php.net/manual/en/function.array-keys.php

use array_keys() to get an array of all the unique keys.

Note that an array with named keys like your $arr can also be accessed with numeric indexes, like $arr[0].

http://php.net/manual/en/function.array-keys.php

小…红帽 2024-12-31 23:59:10

您可以使用 php 的 key 函数来获取密钥名称:

<?php
    $array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'<br />';
      }
    next($array);
     }
?>

如下所示: PHP:key - Manual

you can use key function of php to get the key name:

<?php
    $array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'<br />';
      }
    next($array);
     }
?>

like here : PHP:key - Manual

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文