php 检查一个键是否存在并从PHP中的数组中获取相应的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12268482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:10:54  来源:igfitidea点击:

Check if a key exists and get a corresponding value from an array in PHP

phparrays

提问by Derfder

Any idea how to check if a key exists and if yes, then get the value of this key from an array in php.

知道如何检查键是否存在,如果存在,则从 php 中的数组中获取该键的值。

E.g.

例如

I have this array:

我有这个数组:

$things = array(
  'AA' => 'American history',
  'AB' => 'American cooking'
);

$key_to_check = 'AB';

Now, I need to check if $key_to_checkexists and if it does, get a coresponding value which in this case will be American cooking

现在,我需要检查$key_to_check 是否存在,如果存在,则获取一个相应的值,在这种情况下将是美式烹饪

回答by Mihai Iorga

if(isset($things[$key_to_check])){
    echo $things[$key_to_check];
}

回答by AlTak

I know this question is very old but for those who will come here It might be useful to know that in php7 you can use Null Coalesce Operator

我知道这个问题很老了,但对于那些来这里的人来说,知道在 php7 中可以使用Null Coalesce Operator可能会很有用

if ($value = $things[ $key_to_check ] ?? null) {
      //Your code here
}

回答by JaredMcAteer

if (array_key_exists($key_to_check, $things)) {
    return $things[$key_to_check];
}

回答by Marcin Orlowski

The simpliest approach is to do this:

最简单的方法是这样做:

if( isset( $things[ $key_to_check ]) ) {
   $value = $things[ $key_to_check ];
   echo "key exists. Value: ${value}";
} else {
   echo "no such key in array";
}

And you get the value usual way:

并且您以通常的方式获得价值:

$value = $things[ $key_to_check ];

回答by Sven van Zoelen

Just use isset(), you can use it as follows if you want to use it as an function:

就用isset(),如果你想把它当作一个函数来使用,你可以按如下方式使用它:

function get_val($key_to_check, $array){
    if(isset($array[$key_to_check])) {
        return $array[$key_to_check]);
    }
}

回答by Pedro

isset() will return:
-trueif the key exists and the value is != NULL
-falseif the key exists and value == NULL
-falseif the key does not exist

isset() 将返回:
- - -trueif the key exists and the value is != NULL
falseif the key exists and value == NULL
falseif the key does not exist

array_key_exists() will return:
-trueif the key exists
-falseif the key does not exist

array_key_exists() 将返回:
- -trueif the key exists
falseif the key does not exist

So, if your value may be NULL, the proper way is array_key_exists. If your application doesn't differentiate between NULL and no key, either will work, but array_key_existsalways provides more options.

因此,如果您的值可能为 NULL,则正确的方法是array_key_exists. 如果您的应用程序不区分 NULL 和无键,两者都可以工作,但array_key_exists总是提供更多选项。

In the following example, no key in the array returns NULL, but so does a value of NULL for a given key. That means it's effectively the same as isset.

在以下示例中,数组中没有任何键返回 NULL,但给定键的 NULL 值也会返回 NULL。这意味着它实际上与isset.

The null coalesce operator(??) wasn't added until PHP 7, but this works back to PHP 5, maybe 4:

直到 PHP 7 才添加 null 合并运算符(??),但这可以追溯到 PHP 5,也许是 4:

$value = (array_key_exists($key_to_check, $things) ? $things[$key_to_check] : NULL);

as a function:

作为一个函数:

function get_from_array($key_to_check, $things)
    return (array_key_exists($key_to_check,$things) ? $things[$key_to_check] : NULL);