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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With