PHP - Array Array Searching


in_array function takes two values, the value that you want to search for and the array.

The function returns true if the value is in the array and false otherwise.

array_search function works in the same way except that instead of returning a Boolean, it returns the key where the value is found, or false otherwise.


     $names = ['A', 'B', 'C']; 
     $containsC = in_array('C', $names); 
     var_dump($containsC);            // true 
     $containsSnape = in_array('Snape', $names); 
     var_dump($containsSnape);        // false 
     $wheresRon = array_search('B', $names); 
     var_dump($wheresRon);            // 1 
     $wheresVoldemort = array_search('C', $names); 
?>//w  w w .ja  v  a  2 s . co  m