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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:16:41  来源:igfitidea点击:

array_map and trim not trimming white space from values

phparraystrim

提问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_valueis 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);