public static filter ( array $array, array $filters ) : array | ||
$array | array | Source array |
$filters | array | Rules that define array keys which should be left or removed from results. Each rule is: - `var` - `$array['var']` will be left in result. - `var.key` = only `$array['var']['key'] will be left in result. - `!var.key` = `$array['var']['key'] will be removed from result. |
return | array | Filtered array |
public static function filter($array, $filters)
{
$result = [];
$forbiddenVars = [];
foreach ($filters as $var) {
$keys = explode('.', $var);
$globalKey = $keys[0];
$localKey = isset($keys[1]) ? $keys[1] : null;
if ($globalKey[0] === '!') {
$forbiddenVars[] = [substr($globalKey, 1), $localKey];
continue;
}
if (empty($array[$globalKey])) {
continue;
}
if ($localKey === null) {
$result[$globalKey] = $array[$globalKey];
continue;
}
if (!isset($array[$globalKey][$localKey])) {
continue;
}
if (!array_key_exists($globalKey, $result)) {
$result[$globalKey] = [];
}
$result[$globalKey][$localKey] = $array[$globalKey][$localKey];
}
foreach ($forbiddenVars as $var) {
list($globalKey, $localKey) = $var;
if (array_key_exists($globalKey, $result)) {
unset($result[$globalKey][$localKey]);
}
}
return $result;
}