1

I have an array called $csv. The header columns are contained within $csv[0]. I have a loop that goes in and overwrites some data in the $csv array starting at index 1.

For some reason it is no longer getting any value when I try to output $csv[1]['CRM ID'] even though the value is there when I do a var_export of $csv[1].

I am able to read data out of $csv[1]['Agency'] just fine. Is it something to do with the space? It has been working up until now until a seemingly unrelated piece of 3rd-party code was changed.

// Agency ID - prefix + zerofill
$agency_id_n = strpos($csv[$i]['Agency'], 'A') === 0 ? (int) ltrim($csv[$i]['Agency'], 'A') : (int) $csv[$i]['Agency'];
$csv[$i]['Agency'] = $agency_id_p = 'A' . str_pad($agency_id_n, 5, '0', STR_PAD_LEFT);

// CRM ID - prefix + zerofill
echo "<h1>\$csv :-</h1><pre>" . print_r($csv, true) . "</pre>";
echo "<h1>\$csv[1] :-</h1><pre>" . var_export($csv[1], true) . "</pre>";
echo "<h1>yyyyy = {$csv[1]['CRM ID']} / " . var_export($csv[1]['CRM ID'], true) . "</h1>";

$crm_id_n = strpos($csv[$i]['CRM ID'], 'C') === 0 ? (int) ltrim($csv[$i]['CRM ID'], 'C') : (int) $csv[$i]['CRM ID'];

echo "<h1>has c = " . (strpos($csv[$i]['CRM ID'], 'C') === 0 ? 'yes' : 'no') . "</h1>";
echo "<h1>val = {$csv[$i]['CRM ID']}</h1>";
echo "<h1>y = $crm_id_n</h1>";
die("<h1>csv...</h1><pre>" . print_r($csv, true) . "</pre>");

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • [mcve](https://stackoverflow.com/help/mcve) please, your code is a nightmare to read –  Apr 16 '19 at 02:56
  • @tim It is part of a large piece of code. Surely it's not that hard to follow... – Danny Beckett Apr 16 '19 at 02:57
  • `foreach($csv[1] as $col => $val ){ var_dump($col); }` gives what? Outputting to a browser wont show multiple space characters, thats my guess although a column name changing seems strange – user3783243 Apr 16 '19 at 02:57
  • @user3783243 `string(9) "CRM ID" string(6) "Agency" string(12) "Customer Ref" string(7) "Surname" string(8) "Forename" string(21) ....` – Danny Beckett Apr 16 '19 at 02:59
  • 2
    Ah, a length of `9` ain't right! Seems like there's some dodgy char in there! Nice logic - cheers!! – Danny Beckett Apr 16 '19 at 02:59
  • 2
    `CRM ID` should not be 9 characters. You have some hidden characters there. – user3783243 Apr 16 '19 at 02:59
  • 1
    @user3783243 The `.csv` input file that builds `$csv` was encoded as UTF-8 with BOM rather than UTF8. Since CRM ID is the first column and it takes the key name from `$csv[0]` it had included a BOM in that key name. Thanks for your help! – Danny Beckett Apr 16 '19 at 03:09

1 Answers1

1

The culprit was fgetcsv being susceptible to byte-order marks (BOMs) - from a comment on the PHP manual:

This function has no special BOM handling. The first cell of the first row will inherit the BOM bytes, i.e. will be 3 bytes longer than expected. As the BOM is invisible you may not notice.

Excel on Windows, or text editors like Notepad, may add the BOM.

Community
  • 1
  • 1
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134