2
I need to test if an array has some values that are mandatory by comparing within another array, I currently count the required values, open a loop, and check if the checked values 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 of the first array are required in the second.
The array is one-dimensional.
I didn’t know this function, a much better solution, I just can’t drop a boleano because I need a precise treatment of this false, the code ta mto encapsulated ai will be complicated to treat the exceptions with false where it is called, but solved! obg
– AnthraxisBR