PHP 删除数组中所有字符串元素的空格
要修剪PHP中数组中的所有字符串,代码如下-
示例
<?php $arr = array( " John ", "Jacob ", " Tom ", " Tim "); echo "具有前导和尾随空格的数组...\n"; foreach( $arr as $value ) { echo "Value = $value \n"; } $result = array_map('trim', $arr); echo "\n更新后的数组 ...\n"; foreach( $result as $value ) { echo "Value = $value \n"; } ?>
输出结果
具有前导和尾随空格的数组... Value = John Value = Jacob Value = Tom Value = Tim 更新后的数组 ... Value = John Value = Jacob Value = Tom Value = Tim
示例
现在让我们来看另一个示例-
<?php $arr = array( " Kevin ", "Katie ", " Angelina ", " Jack "); echo "具有前导和尾随空格的数组...\n"; foreach( $arr as $value ) { echo "Value = $value \n"; } array_walk($arr, create_function('&$val', '$val = trim($val);')); echo "\nUpdated Array...\n"; foreach($arr as $key => $value) print($arr[$key] . "\n"); ?>
输出结果
具有前导和尾随空格的数组... Value = Kevin Value = Katie Value = Angelina Value = Jack 更新后的数组 ... Kevin Katie Angelina Jack