0

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").

Community
  • 1
  • 1
user3822168
  • 27
  • 1
  • 6

4 Answers4

2

You need to reference $row in order to make changes to the array (note the &):

foreach($csv as &$row){
    $row = array_map('trim', $row);
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
2

You are not modifying the original array when you do the trim. You should get the values by reference in the foreach loop.

foreach($csv as &$row){
    foreach($row as &$cell){
        $cell = trim($cell);
    }
}

From the documentation:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

sjagr
  • 15,983
  • 5
  • 40
  • 67
0

If you are seeing it like you put here,

" Email" => "Email"

you'll have to trim it from the key, can you show how you are assigning the named headers. Because I would trim it there, but using array_map('trim', $data) will remove the whitespace, I do that all day long.

Well to just clean the keys you can do,

 $keys = array_map( 'trim', array_keys($data) );
 $data = array_combine( $keys, $data );
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • I think his example of `" Email" => "Email"` was his way of describing the original value and the desired value he wants. Not an array structure (although it may seem like it because of his usage of `=>`) – sjagr Jul 23 '14 at 15:54
  • Then why doesn't array map work, I do this 100's of times, Well I really don't but because I have a splfileobj csv reader, I should publish that by the way lol. – ArtisticPhoenix Jul 23 '14 at 15:56
  • He forgot to pass the iterated values by reference, so he wasn't trimming the original array. – sjagr Jul 23 '14 at 15:57
  • That's irrelevant for using array_map('trim', $data); as OP said they tried, there is other issues going on here, what those are is impossible to tell without more code. – ArtisticPhoenix Jul 23 '14 at 16:01
0
$input = [
    'parent' => [
        'child' => ' element to be trimmed '
    ]
];
array_walk_recursive($input, function (&$item) {
    if (!is_array($item)) {
        $item = trim($item);
    }
});
Erald Karakashi
  • 860
  • 8
  • 5