示例:
$array = [" apple ", "banana ", " orange"];
$trimmedArray = array_map('trim', $array);
print_r($trimmedArray);
輸出:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
示例:
function customTrim($string) {
return trim($string, '/');
}
$array = ["/apple", "banana/", "/orange/"];
$trimmedArray = array_map('customTrim', $array);
print_r($trimmedArray);
輸出:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
示例:
$array = ["apple", "", "banana", " ", "orange"];
$trimmedArray = array_filter($array, function($value) {
return trim($value) !== '';
});
print_r($trimmedArray);
輸出:
Array
(
[0] => apple
[2] => banana
[4] => orange
)