Probably, one of the most used functions with arrays is in_array
. This 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. This is very useful, because a lot
of times what you want to know from a list or a map is if it
contains an element, rather than knowing that it does or its
location.
Even more useful sometimes is array_search
. This 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. Let's see both functions:
<?php $names = ['Harry', 'Ron', 'Hermione']; $containsHermione = in_array('Hermione', $names); var_dump($containsHermione); // true $containsSnape = in_array('Snape', $names); var_dump($containsSnape); // false $wheresRon = array_search('Ron', $names); var_dump($wheresRon); // 1 $wheresVoldemort = array_search('Voldemort', $names); var_dump($wheresVoldemort); // false