Recursively check if value exits in array

The native in_array php function is very handy to check if a value exists in an array. But when you are working with multidimensional arrays it does not work anymore.

When you want to check if your value exists in the multidimensional array, you can use the following helper function to acomplish that.


 * Enhancement for the native php function in_array.
 * It can now recursively check if a value exists
 * in a given array.
 * @param $needle
 * @param $haystack
 * @param bool $strict
 * @return bool
function in_array_r($needle, $haystack, $strict = false)
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;

    return false;