- implode: documentation ( source)
- define: documentation ( source)
- sprintf: documentation ( source)
<?php
$e['invalid']['key'][] = 'a123';
$e['invalid']['key'][] = 'a456';
$e['invalid']['color'][] = 'red';
$e['missing']['key'][] = 'b72';
$e['missing']['color'][] = 'blue';
$e['missing']['color'][] = 'green';
define("ERROR_LOOKUP", [
'invalid' => '%s is not a valid %s',
'missing' => '%s is a required %s - other functions have dependencies on it',
]);
$errors = [];
foreach ($e as $type => $subtypes) {
if (!isset(ERROR_LOOKUP[$type])) {
continue;
}
foreach ($subtypes as $subtype => $entry) {
foreach ($entry as $string) {
$errors[] = sprintf(ERROR_LOOKUP[$type], $subtype, $string);
}
}
}
echo implode("\n", $errors);