Here's some test data in an associative array for which I'm trying to build a trim function:
$header = [
'comment1' => ' abc ',
'comment2' => 'abc ',
'comment3' => ' abc',
'comment4' => 'abc'
];
The foreach
line code below works correctly to trim the values within the array and keep the original keys:
echo '<pre>' . print_r($header, 1) . '</pre>';
var_dump($header);
foreach ($header as &$val) $val = trim($val); // <--- this is the line that does the trimming
echo '<pre>' . print_r($header, 1) . '</pre>';
var_dump($header);
However, when I attempt to make the foreach
into a function like so
function trimValues($array) {
foreach ($array as &$value) {
$value = trim($value);
}
}
And call it like this
echo '<pre>' . print_r($header, 1) . '</pre>';
var_dump($header);
trimValues($header); // <--- this is the line that calls the trimming function
echo '<pre>' . print_r($header, 1) . '</pre>';
var_dump($header);
The output shows the trim didn't work
What am I overlooking here?