php 如何在php中修剪数组值的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5762439/
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
How to trim white spaces of array values in php
提问by n92
I have an array as follows
我有一个数组如下
$fruit = array(' apple ','banana ', ' , ', ' cranberry ');
I want an array which contains the values without the white spaces on either sides but it can contain empty values how to do this in php.the output array should be like this
我想要一个数组,其中包含两边没有空格的值,但它可以包含空值如何在 php.the 中执行此操作。输出数组应该是这样的
$fruit = array('apple','banana', ',', 'cranberry');
回答by Shakti Singh
回答by Shiv Kumar Sah
array_walk()
can be used with trim()
to trim array
array_walk()
可用于trim()
修剪数组
<?php
function trim_value(&$value)
{
$value = trim($value);
}
$fruit = array('apple','banana ', ' cranberry ');
var_dump($fruit);
array_walk($fruit, 'trim_value');
var_dump($fruit);
?>
See 2nd example at http://www.php.net/manual/en/function.trim.php
回答by Elnoor
If the array is multidimensional, this will work great:
如果数组是多维的,这会很好用:
//trims empty spaces in array elements (recursively trim multidimesional arrays)
function trimData($data){
if($data == null)
return null;
if(is_array($data)){
return array_map('trimData', $data);
}else return trim($data);
}
one sample test is like this:
一个样本测试是这样的:
$arr=[" aaa ", " b ", "m ", [" .e ", " 12 3", "9 0 0 0 "]];
print_r(trimData($arr));
//RESULT
//Array ( [0] => aaa [1] => b [2] => m [3] => Array ( [0] => .e [1] => 12 3 [2] => 9 0 0 0 ) )
回答by Branimir Viljevac
If you want to trim and printone dimensional Array or the deepest dimension of multi-dimensional Array you should use:
如果要修剪和打印一维数组或多维数组的最深维度,则应使用:
foreach($array as $key => $value)
{
$array[$key] = trim($value);
print("-");
print($array[$key]);
print("-");
print("<br>");
}
If you want to trim but do not want to printone dimensional Array or the deepest dimension of multi-dimensional Array you should use:
如果要修剪但不想打印一维数组或多维数组的最深维度,则应使用:
$array = array_map('trim', $array);
回答by Xenox
Multidimensional-proof solution:
多维解决方案:
array_walk_recursive($array, function(&$arrValue, $arrKey){ $arrValue = trim($arrValue);});
回答by Goose
I had trouble with the existing answers when using multidimensional arrays. This solution works for me.
使用多维数组时,我在现有答案上遇到了麻烦。这个解决方案对我有用。
if (is_array($array)) {
foreach ($array as $key => $val) {
$array[$key] = trim($val);
}
}
回答by Abolfazl Sharifi
simply you can use regex to trim all spaces or minify your array items
只需使用正则表达式即可修剪所有空格或缩小数组项
$array = array_map(function ($item) {
return preg_replace('/\s+/', '', $item);
}, $array);
回答by Jesús
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function generateRandomSpaces() {
$output = '';
$i = rand(1, 10);
for ($j = 0; $j <= $i; $j++) {
$output .= " ";
}
return $output;
}
// Generating an array to test
$array = [];
for ($i = 0; $i <= 1000; $i++) {
$array[] = generateRandomSpaces() . generateRandomString(10) . generateRandomSpaces();
}
// ARRAY MAP
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$trimmed_array=array_map('trim',$array);
}
$time = (microtime(true) - $start);
echo "Array map: " . $time . " seconds\n";
// ARRAY WALK
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
array_walk($array, 'trim');
}
$time = (microtime(true) - $start);
echo "Array walk : " . $time . " seconds\n";
// FOREACH
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
foreach ($array as $index => $elem) {
$array[$index] = trim($elem);
}
}
$time = (microtime(true) - $start);
echo "Foreach: " . $time . " seconds\n";
// FOR
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
for ($j = 0; $j < count($array) - 1; $j++) {
$array[$j] = trim($array[$j]);
}
}
$time = (microtime(true) - $start);
echo "For: " . $time . " seconds\n";
The output of the code above is:
上面代码的输出是:
Array map: 8.6775720119476 seconds
Array walk: 10.423238992691 seconds
Foreach: 7.3502039909363 seconds
For: 9.8266389369965 seconds
数组映射:8.6775720119476 秒
Array walk:10.423238992691 秒
Foreach:7.3502039909363 秒
For:9.8266389369965 秒
This values of course may change but I would say foreach is the best option.
这个值当然可能会改变,但我会说 foreach 是最好的选择。
回答by Aistis
array_map('trim', $data)
would convert all subarrays into null
. If it is needed to trim spaces only for strings and leave other types as it is, you can use:
array_map('trim', $data)
会将所有子数组转换为null
. 如果只需要修剪字符串的空格并保留其他类型,则可以使用:
$data = array_map(
function ($item) {
return is_string($item) ? trim($item) : $item;
},
$data
);
回答by Piotr Nowak
Trim in array_map change type if you have NULL in value.
如果值为 NULL,则在 array_map 更改类型中进行修剪。
Better way to do it:
更好的方法:
$result = array_map(function($v){
return is_string($v)?trim($v):$v;
}, $array);