Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: Case-insensitive in_array()
<?php
/**
* Case-insensitive in_array() wrapper.
*
* @param mixed $needle Value to seek.
* @param array $haystack Array to seek in.
*
* @return bool
*/
function in_arrayi($needle, $haystack)
{
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
@adamcameron

This comment has been minimized.

Copy link

@adamcameron adamcameron commented Apr 1, 2016

Thanks for publicising this. Was racking my brains how to do this, and didn't realise one could use built-in functions with array_ap. Nice one!

@raph8888

This comment has been minimized.

Copy link

@raph8888 raph8888 commented Dec 8, 2017

Sweet.

@bls1999

This comment has been minimized.

Copy link

@bls1999 bls1999 commented Dec 11, 2017

Not the hero we need, the hero we deserve. Well done, my friend.

@ZamBoyle

This comment has been minimized.

Copy link

@ZamBoyle ZamBoyle commented Apr 19, 2021

Very clever use of the callback parameter. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment