php - 如何使用 in_array 获取数组中的值而不是 true?
问题描述
我怎么能收到返回的值是数组内的值,而不是真的?in_array() 这样做吗?
$type = 'ai';
$types = ['analitycs', 'log_in_info', 'auto_dark_mode', 'ai', 'notification'];
echo in_array($type, $types); //my wish "ai", my reality "1";
解决方案
您可以搜索它,返回密钥并使用该密钥访问它:
echo $types[array_search($type, $types)];
或者如果$type
找到你可以访问它:
echo in_array($type, $types) ? $type : 'not found'; // or whatever
推荐阅读
- iot - 使用 Cumulocity 将用户添加到组
- react-native - 应用重新加载后不显示来自本地 uri (file:///[...]/cache/[...].jpg) 的图像
- python-3.x - 从 excel 表格到 OrderedDict
- pandas - 如果满足条件,两列的差异
- c# - ServiceStack 试图绑定通过服务引用而不是公共属性生成的私有字段类
- ruby - 如何在红宝石中初始化winium?
- excel - 选择最小范围?
- javascript - 将文本类型输入更改为格式化密码
- java - 在 Jboss 上为 JAX-RS 2.0 / Resteasy 客户端设置超时
- c# - 如何在 WPF 中存储 DatePicker 中的值?