Checking if a value exists?
if (in_array('admin', $roles, true)) {
// true === strict check, avoids weird type bugs
}
Need to find the key instead?
$key = array_search('editor', $roles, true);
// Returns: 'editor'
Always use the strict mode (true) – otherwise, PHP might return unexpected results due to loose comparisons (like “0" == false).
Pro tip: If you do lots of lookups, consider flipping the array so the value becomes the key – that’s way faster.
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter