如何对每个数组项运行php过滤器,保留数组中传递的项


How do I run a php filter against each array item, keeping the items that pass in the array?

我有一个值数组,我想运行php的内置电子邮件验证过滤器。如何针对每个数组项运行过滤器,保留数组中传递的项?

使用php的array_filter: http://php.net/manual/en/function.array-filter.php

示例(更新使用FILTER_VALIDATE_EMAIL):

function validate_email($email_address)
{
   return filter_var($email_address, FILTER_VALIDATE_EMAIL);
}
$email_addresses = array("me@example.com", "123");
$email_addresses = array_filter($email_addresses, "validate_email");

现在,$email_addresses将只包含me@example.com