Home  >  Article  >  Backend Development  >  Does php two-dimensional array have a certain value?

Does php two-dimensional array have a certain value?

尚
Original
2019-10-30 14:35:063398browse

Does php two-dimensional array have a certain value?

PHP determines whether the two-dimensional array contains a certain value:

PHP can use loop traversal to compare each value in the two-dimensional array with the query Values ​​are compared to determine whether a certain value is contained in the two-dimensional array.

$arr = array(  
   array('a', 'b'),  
   array('c', 'd')  
);  
    
in_array('a', $arr); // 此时返回的永远都是 false  
deep_in_array('a', $arr); // 此时返回 true 值  
    
function deep_in_array($value, $array) {   
    foreach($array as $item) {   
        if(!is_array($item)) {   
            if ($item == $value) {  
                return true;  
            } else {  
                continue;   
            }  
        }   
            
        if(in_array($value, $item)) {  
            return true;      
        } else if(deep_in_array($value, $item)) {  
            return true;      
        }  
    }   
    return false;   
}

Recommended: php server

The above is the detailed content of Does php two-dimensional array have a certain value?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn