我有一个数组如下所示

$fruit = array('  apple ','banana   ', ' , ',     '            cranberry ');

我想要一个数组,其中包含的值没有空白两侧,但它可以包含空值如何在php中做到这一点。输出数组应该是这样的

$fruit = array('apple','banana', ',', 'cranberry');

当前回答

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);

?>

参见http://www.php.net/manual/en/function.trim.php上的第二个例子

其他回答

Array_map和trim可以完成这项工作

$trimmed_array = array_map('trim', $fruit);
print_r($trimmed_array);

如果数组是多维的,这将工作得很好:

//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);
}

一个例子是这样的:

$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 ) )

Multidimensional-proof解决方案:

array_walk_recursive($array, function(&$arrValue, $arrKey){ $arrValue = trim($arrValue);});

Array_map ('trim', $data)将所有子数组转换为null。如果只需要为字符串修剪空格,而保持其他类型不变,你可以使用:

$data = array_map(
    function ($item) {
        return is_string($item) ? trim($item) : $item;
    },
    $data
);
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";

上面代码的输出是:

数组映射:8.6775720119476秒 数组行走:10.423238992691秒 Foreach: 7.3502039909363秒 对于:9.8266389369965秒

这个值当然可能会改变,但我想说foreach是最好的选择。