我有这样一个数组:
Array
(
[0] => "<one@one.com>"
[1] => "<two@two.co.in>"
[2] => "<three@hello.co.in>"
)
Run Code Online (Sandbox Code Playgroud)
现在我想删除"<"和">"从上面的数组,使它看起来像
Array
(
[0] => "one@one.com"
[1] => "two@two.co.in"
[2] => "three@hello.co.in"
)
Run Code Online (Sandbox Code Playgroud)
如何在PHP中执行此操作?请帮帮我.
我正在使用array_filter(); 有没有更简单的方法来做到这一点,除了array_filter()?
你可以在上面使用array_walk:
// Removes starting and trailing < and > characters
function trim_gt_and_lt(&$value)
{
$value = trim($value, "<>");
}
array_walk($array, 'trim_gt_and_lt');
Run Code Online (Sandbox Code Playgroud)
但请注意,这也将删除可能不是您想要的开始>和结尾<.
首先,如果你想改变价值,那就是array_map()你想要的,而不是array_filter().array_filter()选择性地删除或保留数组条目.
$output = array_map('remove_slashes', $input);
function remove_slashes($s) {
return preg_replace('!(^<|>$)!', '', $s);
}
Run Code Online (Sandbox Code Playgroud)
当然,您也可以使用简单的foreach循环来完成此操作.