3

I am having trouble accessing the first index of an associative array when parsing from a csv.

CSV:

ID,COLOR
1,Red
2,Green 
3,Blue

PHP:

function associative_array_from_csv($csv)
{
    $rows   = array_map('str_getcsv', file($csv));
    $header = array_shift($rows);
    $array  = array();

    foreach($rows as $data) {
        $array[] = array_combine($header, $data);
    }

    return $array;
}

$colors = associative_array_from_csv($csv);

Now $colors returns:

[
    [
        "ID"    => "1",
        "COLOR" => "Red",
    ],
    [
        "ID"    => "2",
        "COLOR" => "Green ",
    ],
    [
        "ID"    => "3",
        "COLOR" => "Blue",
    ],
];

But if I try to access the ID of any color:

$colors[0]["ID"]    // returns undefined index: ID
$colors[0]["COLOR"] // returns "Red"

If I loop over the colors I can access the ID like so:

foreach($colors as $color) 
{ 
    print_r(reset($color)); // Prints the ID 
}

But why I can't access it directly like $colors[0]["ID"]?

Thanks

4
  • is it $color or $colors? Commented Apr 14, 2017 at 15:45
  • Works for me: 3v4l.org/HTcTI, please check. Commented Apr 14, 2017 at 15:47
  • 2
    Try trimming your array keys, there may be some weird characters in there. Commented Apr 14, 2017 at 15:56
  • @AmrAly It's $colors, I've updated that last line. Commented Apr 14, 2017 at 16:22

2 Answers 2

10

I had the same issue. The problem was that Excel adds a hidden character to the first cell in the document, for encoding UTF-8 BOM.

I could see this when running:

var_dump(json_encode($array));

This would return:

string(312) "[{"\ufeffemail":"[email protected]","firstName":"import","lastName":"test"},{"\ufeffemail":"[email protected]","firstName":"import","lastName":"test"}]"

To remove the \ufeff character, I did:

$header = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $header);

In this case, that would be:

function associative_array_from_csv($csv)
{
    $rows   = array_map('str_getcsv', file($csv));
    $header = array_shift($rows);
    $header = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $header);
    $array  = array();

    foreach($rows as $data) {
        $array[] = array_combine($header, $data);
    }

    return $array;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works very well. You saved my night. I'm so confused why my first index is undefined. Big thanks to questionnaire and you.
1

This worked for me!

header = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $header);

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Missing $ at begging :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.