2

I'm seeing a strange issue with some (should be simple) PHP code in a basic script.

I have a multidimensional associative array $accounts that looks like this when var_dump is used:

    Array(4) {
      [0] =>
      array(3) {
        'account' =>
        string(37) "Flood Cleanup City - Desktop - Exact "
        'parameter' =>
        string(23) "flood_cleanup_city_d_em"
        'phone' =>
        string(0) ""
      }
      [1] =>
      array(3) {
        'account' =>
        string(51) "Flood Cleanup City - Desktop - Exact Call Extension"
        'parameter' =>
        string(3) "N/A"
        'phone' =>
        string(0) ""
      }
      [2] =>
      array(3) {
        'account' =>
        string(38) "Flood Cleanup City - Desktop - Phrase "
        'parameter' =>
        string(23) "flood_cleanup_city_d_pm"
        'phone' =>
        string(0) ""
      }
      [3] =>
      array(3) {
        'account' =>
        string(52) "Flood Cleanup City - Desktop - Phrase Call Extension"
        'parameter' =>
        string(3) "N/A"
        'phone' =>
        string(0) ""}
}

So, straightforward enough. This array is generated in a function and passed as the return value to a variable $listAccounts.

I just want to iterate over $listAccounts and extract the 'account' value, so I wrote this:

foreach($listAccount as $account)
{
    $accountName = $account['account'];
    echo $accountName;
}

I'd expect it would output the four account strings. Instead, $account['account'] is returning NULL. BUT, if I use the array_keys function to extract the names of the keys from the array and use this code, it works properly:

$accountName = $account[array_keys($account)[0]];

In case it might be relevant, the function that generates the multidimensional array is using the fgetcsv() function to parse a CSV file:

function getAccounts()
{
    $handle = fopen("water.csv","r");
    $header = NULL;
    $accounts = array();
    $n = 0;
    while (!feof($handle)) {
        $account = fgetcsv($handle);
        if(!$header)
        {
            $header = $account;
        }
        else
        {
            $accounts[] = array_combine($header,$account);
        }
    }
    fclose($handle);
    echo var_dump($accounts);
    return $accounts;
}
Joe
  • 23
  • 2
  • You may not think the dupe link is related, but its directly related as toor eluded too. You have a BOM in that file, which is being read with the first header field in `fgetcsv`. And you need to get rid of it before doing the csv parsing. – IncredibleHat Mar 09 '18 at 15:32

1 Answers1

1

You have a strange first invisible symbol in the "account" key name. Please filter data after parsing the CSV file.

toor
  • 101
  • 5
  • 1
    The hex code of that character is: `efbbbf` ... which is the BOM (Byte Order Mark). If at all possible its best to save the .csv without it. But if you cannot, then pull that off the front of the file before parsing (as trim won't get rid of easily). – IncredibleHat Mar 09 '18 at 15:28
  • Thank you both! That was it. I'm using PhpStorm and was able to use the "Remove the BOM" option on the CSV file from directly inside of the IDE. Thank you! – Joe Mar 09 '18 at 17:56