I'm trying to parse a CSV file and as part of it I would like to remove leading/trailing whitespace from all of my cells. Since it's a CSV file it's formatted like a 2D array. Initially I tried:
foreach($csv as $row){
foreach($row as $cell){
$cell = trim($cell);
}
}
However the result was untrimmed.
Next I tried using array_map as suggested here.
$csv = array_map('trim', $csv);
This gave me back an array of empty rows. So I also tried
foreach($csv as $row){
$row = array_map('trim', $row);
}
Which like my first attempt didn't change anything.
Here's the CSV data I'm using as my input:
First Name,Last Name,Contact Method,Phone, Email John,Doe,Email,1-XXX-XXX-XXXX, john@example.com Jane,Doe,Phone Call,1-XXX-XXX-XXXX,jane@example.com
In particular I was trying to get my script to trim the leading space in the last cell of the first row (" Email" => "Email").