PHP multidimensional array search by value

前端 未结 23 3328
情歌与酒
情歌与酒 2020-11-21 04:45

I have an array where I want to search the uid and get the key of the array.

Examples

Assume we have the following 2-dimensional array:

<
23条回答
  •  囚心锁ツ
    2020-11-21 05:40

    /**
     * searches a simple as well as multi dimension array
     * @param type $needle
     * @param type $haystack
     * @return boolean
     */
    public static function in_array_multi($needle, $haystack){
        $needle = trim($needle);
        if(!is_array($haystack))
            return False;
    
        foreach($haystack as $key=>$value){
            if(is_array($value)){
                if(self::in_array_multi($needle, $value))
                    return True;
                else
                   self::in_array_multi($needle, $value);
            }
            else
            if(trim($value) === trim($needle)){//visibility fix//
                error_log("$value === $needle setting visibility to 1 hidden");
                return True;
            }
        }
    
        return False;
    }
    

提交回复
热议问题