PHP - Array Array Searching

Introduction

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.

Demo

<?php
     $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); 
     var_dump($wheresVoldemort);      
?>//  w  w  w  .  ja va2  s.  c  o  m

Result