php check if array has field code example
Example 1: check if array has value php
$myArr = [38, 18, 10, 7, "15"];
echo in_array(10, $myArr);
echo in_array(19, $myArr);
echo in_array("18", $myArr);
echo in_array("18", $myArr, true);
Example 2: php check if item in array
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType))
Example 3: keys of array exist in array
function findKey($array, $keySearch)
{
foreach ($array as $key => $item) {
if ($key == $keySearch) {
echo 'yes, it exists';
return true;
} elseif (is_array($item) && findKey($item, $keySearch)) {
return true;
}
}
return false;
}