0

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

Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
  • Please provide sample input as `var_export()`. – mickmackusa Jan 25 '20 at 05:47
  • @mickmackusa I added a link to the 3v4l of the problem in question, it may be easier to review that. There are a few variables in the mix here. – Barry Chapman Jan 25 '20 at 05:48
  • 1
    array_filter only checks the top level, you need filter by the second level. There is a duplicate on this site. – mickmackusa Jan 25 '20 at 05:50
  • 1
    @BarryChapman I think it's probably easier to avoid setting the array entries in the first place. Try something like this: https://3v4l.org/UAfng – Nick Jan 25 '20 at 05:55
  • https://3v4l.org/hddlX (ha, I totally agree with Nick! ...I am slower on my phone.) Unless you actually need the counts of each category, there are cleaner ways to check password strength. – mickmackusa Jan 25 '20 at 06:03
  • 1
    @Barry Consider https://3v4l.org/Ys5cG which is a modified version of https://stackoverflow.com/a/5142164/2943403 – mickmackusa Jan 25 '20 at 06:31
  • @mickmackusa I needed the array data to tell the user which requirements had been fulfilled – Barry Chapman Jan 25 '20 at 07:20
  • @mickmackusa i suppose i can just examine the capture groups – Barry Chapman Jan 25 '20 at 07:21
  • Those are lookaheads, not capture groups -- they will not provide the detail that you seek. – mickmackusa Jan 25 '20 at 07:21
  • So what do you propose i do then? – Barry Chapman Jan 25 '20 at 07:22
  • Personally, I would not bother scripting my app to memorize then display portions of a disqualified password. I would be displaying a single static message that explains all of the criteria for a qualifying password. If a user cannot, obey a simple set of rules, how can I be sure that they understand how to click a hyperlink. Yes, we (devs) are always bending over backwards to offer a premium UX, but I think you are folding yourself in half and making your job harder than it needs to be. Your password validation can sensibly return a pass/fail response. – mickmackusa Feb 06 '20 at 22:34
  • Otherwise, you can write 4 `preg_match()` calls. In fact, from a UX point of view, you can write a js real-time validator that expresses the satisfaction of your criteria. Some web forms have a password strength indicator; maybe that is interesting to you. – mickmackusa Feb 06 '20 at 22:36

0 Answers0