Skip to Content
Menu

in_array_r()

Recursive search for traversing multidimensional arrays.

PHP June 22, 2017

Usage

PHP
nebula()->in_array_r($needle, $haystack, $strict)

Parameters

$needle
(Required) (String) The string to look for
Default: None

$haystack
(Required) (Array) The array to look in
Default: None

$strict
(Optional) (Boolean/String) The type and value must both match, or "contains" for partial string checking
Default: true

Parameter Notes

Pass 'contains' for $strict to for a partial string check (using stripos).

Request or provide clarification »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 840.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_in_array_r"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function in_array_r($haystack, $needles, $strict=true){
                $override = apply_filters('pre_in_array_r', null, $haystack, $needles, $strict);
                if ( isset($override) ){return $override;}
    
                foreach ( $needles as $needle ){
                    if ( $strict === true ){ //If strict, match the type and the value
                        if ( $needle === $haystack ){
                            return true;
                        }
                    } else {
                        if ( $strict === 'contains' ){ //If strict is 'contains', check if the item contains the needle
                            if ( stripos($haystack, $needle) !== false ){
                                return true;
                            }
                        } elseif ( $$needle === $haystack ){ //Otherwise check if the item matches the needle (regardless of type)
                            return true;
                        }
                    }
    
                    if ( is_array($needle) && in_array_r($haystack, $needle, $strict) ){ //If the item is an array, recursively check that array
                        return true;
                    }
                }
    
                return false;
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_in_array_r', 'my_custom_in_array_r', 10, 4); //The last integer must be 1 more than the actual parameters
    function my_custom_in_array_r($null, $needle, $haystack, $strict){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_in_array_r', '__return_false');