Skip to content
Advertisement

How to get the value that is in the array instead of true with in_array?

How could I receive the return with the value that is inside the array, and not true? in_array() does this?

$type = 'ai';

$types = ['analitycs', 'log_in_info', 'auto_dark_mode', 'ai', 'notification'];

echo in_array($type, $types); //my wish "ai", my reality "1";

Advertisement

Answer

You could search for it, that returns the key and use that key to access it:

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

Or if $type is found you can just access it:

echo in_array($type, $types) ? $type : 'not found';  // or whatever
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement