Does php two-dimensional array have a certain value?

Release: 2023-02-28 07:06:02
Original
3396 people have browsed it

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;   
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template