如何使用PHP从数组中删除空格? [英] How do you strip whitespace from an array using PHP?

查看:250
本文介绍了如何使用PHP从数组中删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用PHP从数组中删除空格?

I was wondering how can I strip white space from an array using PHP?

推荐答案

您可以组合使用

  • array_filter — Filters elements of an array using a callback function
  • array_map — Applies the callback to the elements of the given arrays
  • trim — Strip whitespace (or other characters) from the beginning and end of a string

代码:

array_filter(array_map('trim', $array));

这将从侧面删除所有空格(但不能在字符之间删除).它将删除输入等于FALSE的所有条目(例如0、0.00,null,false等)

This will remove all whitespace from the sides (but not between chars). And it will remove any entries of input equal to FALSE (e.g. 0, 0.00, null, false, …)

示例:

$array = array(' foo ', 'bar ', ' baz', '    ', '', 'foo bar');
$array = array_filter(array_map('trim', $array));
print_r($array);

// Output
Array
(
    [0] => foo
    [1] => bar
    [2] => baz
    [5] => foo bar
)

这篇关于如何使用PHP从数组中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆