Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Associative array email check

Tags:

php

I have an array multidimensional associative array.

Array
(


[0] => Array
        (
            [username] => uname1
            [name] => fullname1
            [email] => [email protected]

        )
[1] => Array
        (
            [username] => uname2
            [name] => fullname2
            [email] => uname2

        )
[2] => Array
        (
            [username] => uname3
            [name] => fullname3
            [email] => uname3@email

        )
[3] => Array
        (
            [username] => uname4
            [name] => fullname4
            [email] => uname4@

        )

}

It should validate email address using the regular expression.The return array should consists of an array with only valid array.The array should be

Array
(


[0] => Array
        (
            [username] => uname1
            [name] => fullname1
            [email] => [email protected]
}

since [1,2,3] have invalid email address.

like image 318
mark rammmy Avatar asked Dec 05 '25 15:12

mark rammmy


1 Answers

You can use array_filter to apply a function on each value of an array and filter those values out where the function returns a false value. And to validate the email address, you can use filter_var in combination with FILTER_VALIDATE_EMAIL:

function filter_email($item) {
    return isset($item['email']) && filter_var($item['email'], FILTER_VALIDATE_EMAIL);
}
$filtered = array_filter($arr, 'filter_email');
like image 60
Gumbo Avatar answered Dec 07 '25 03:12

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!