首页 > 解决方案 > 如何使用 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";

标签: php

解决方案


您可以搜索它,返回密钥并使用该密钥访问它:

echo $types[array_search($type, $types)];

或者如果$type找到你可以访问它:

echo in_array($type, $types) ? $type : 'not found';  // or whatever

推荐阅读