php array_map 和 trim 不从值中修剪空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18275921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
array_map and trim not trimming white space from values
提问by Anagio
This function below returns a string of values comma separated
下面的这个函数返回一串逗号分隔的值
$key_1_value = get_post_meta(422,'keywords',true);
$key_1_value = get_post_meta(422,'keywords',true);
The output in my browser looks like
red, white, blue, blue two , green, yellow, purple, magenta , cyan, black
我的浏览器中的输出看起来像
red, white, blue, blue two , green, yellow, purple, magenta , cyan, black
I'm trying to trim the white space before and after all values.
我试图在所有值之前和之后修剪空白。
So I used this code to try and trim the whitespace but it's still there. Why won't this trim the values?
所以我用这段代码来尝试修剪空白,但它仍然存在。为什么这不会修剪值?
$test = array($key_1_value);
$trimmed_array=array_map('trim',$test);
print_r($trimmed_array);
回答by Hanky Panky
$key_1_value
is a string representation and not an array or a string with quoted values, you have to explode it into array items, and not just put it inside an array call, then it becomes a proper array
$key_1_value
是字符串表示,而不是数组或带引号值的字符串,您必须将其分解为数组项,而不仅仅是将其放入数组调用中,然后它就变成了正确的数组
$test = explode(",",$key_1_value);
$trimmed_array=array_map('trim',$test);
print_r($trimmed_array);