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

查看:17
本文介绍了检查一个键是否存在并从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天全站免登陆