I need to test if an array has some values that are mandatory comparing inside another array, I currently count the required values, loop, and check if the values checked were in the same amount:
private function validate_required($data, $required) {
$c = count($required);
$a = 0;
foreach ($data as $key) {
if (in_array($key, $required)) {
$a += 1;
}
}
if ($a != $c) {
throw new \Exception('O elemento {elemento} não possui todos os atributos obrigatórios', 'ex0893');
}
return true;
}
What would be a more natural way to do this test?
OBS :
Not all values in the first array are mandatory in the second array.
The array is one-dimensional.