php 如何修剪数组中的所有字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6769464/
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-26 01:11:56  来源:igfitidea点击:

How can I trim all strings in an Array?

phparraystrim

提问by Daniel

If I have this array:

如果我有这个数组:

array("  hey  ", "bla  ", "  test");

and I want to trim all of them, How can I do that?

我想修剪所有这些,我该怎么做?

The array after the trim:

修剪后的数组:

array("hey", "bla", "test");

回答by zerkms

array_map()is what you need:

array_map()是你需要的:

$result = array_map('trim', $source_array);

回答by KingCrunch

array_map()applies a given callback to every value of an array and return the results as a new array.

array_map()将给定的回调应用于数组的每个值,并将结果作为新数组返回。

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