检查键是否存在,并从PHP的数组中获取相应的值 [英] Check if a key exists and get a corresponding value from an array in PHP

查看:80
本文介绍了检查键是否存在,并从PHP的数组中获取相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何检查某个键是否存在以及是否存在,然后从php中的数组获取此键的值.

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

例如

我有这个数组:

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

$key_to_check = 'AB';

现在,我需要检查 $ key_to_check 是否存在,并获得一个对应的值,在这种情况下,该值将为美式烹饪

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

推荐答案

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

这篇关于检查键是否存在,并从PHP的数组中获取相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆