I have an initialized array defined as such:
$validations = [
'NUMERIC' => false,
'AL_LOWER' => false,
'AL_UPPER' => false,
'SPECIAL' => false
];
There is a function that effectively calls preg_match_all
and passes each of those keys as a store for the matches:
preg_match_all('/([0-9]+)/', $password, $validations['NUMERIC']);
preg_match_all('/([a-z]+)/', $password, $validations['AL_LOWER']);
preg_match_all('/([A-Z]+)/', $password, $validations['AL_UPPER']);
preg_match_all('/([!@#$%^&*.,\[\]\-_]+)/', $password, $validations['SPECIAL']);
So given the password 'MyPassword', there would be two out of the four validations matching. The matches and regex work fine, but the hangup I have is with the filtering of the resulting array:
Array
(
[NUMERIC] => Array
(
[0] => Array
(
)
[1] => Array
(
)
)
[AL_LOWER] => Array
(
[0] => Array
(
[0] => y
[1] => assword
)
[1] => Array
(
[0] => y
[1] => assword
)
)
[AL_UPPER] => Array
(
[0] => Array
(
[0] => M
[1] => P
)
[1] => Array
(
[0] => M
[1] => P
)
)
[SPECIAL] => Array
(
[0] => Array
(
)
[1] => Array
(
)
)
)
As you see, there are empty array items dumped in each of the validation
keys in the array. Using array_filter
to filter out the non-truthys does nothing here, and just returns the same array as the keys have corresponding non-empty values.
Using array_map
I attempted using array_map()
with array_filter()
as a callable, and then again using count()
:
$output = array_map('array_filter', $validations);
$count = array_map('count', $output);
$countSum = array_sum($count);
However, no matter how much I reiterate that filter, it does not filter out those empty arrays:
Array
(
[NUMERIC] => Array
(
)
[AL_LOWER] => Array
(
[0] => Array
(
[0] => y
[1] => assword
)
[1] => Array
(
[0] => y
[1] => assword
)
)
[AL_UPPER] => Array
(
[0] => Array
(
[0] => M
[1] => P
)
[1] => Array
(
[0] => M
[1] => P
)
)
[SPECIAL] => Array
(
)
)
In this case, the keys NUMERIC
and SPECIAL
should be stripped out. I feel like I am missing something fundamental here, but am at a loss as to what.
How can I filter out those empty array elements in the resulting array?
EDIT
This is a link to a 3v4l of the item in question: https://3v4l.org/erDji