0

I have an numeric index array here is a portion of the print_r:

Array
(
    [0] => Array
        (
            [from_stop_id] => 1
            [to_stop_id] => 1
            [transfer_type] => 1
            [min_transfer_time] => 
        )

    [1] => Array
        (
            [from_stop_id] => 3
            [to_stop_id] => 3
            [transfer_type] => 1
            [min_transfer_time] => 
        )

    [2] => Array
        (
            [from_stop_id] => 4
            [to_stop_id] => 4
            [transfer_type] => 1
            [min_transfer_time] => 
        )
)

here is my php loop:

for ( $counter = 0; $counter < count($transfers_csv); $counter++) {
    echo $transfers_csv[$counter]['from_stop_id'];
    echo $transfers_csv[$counter]['to_stop_id'];
    echo  $transfers_csv[$counter]['transfer_type']; 
    echo $transfers_csv[$counter]['min_transfer_time'];
}

here is my error output:

Notice: Undefined index: from_stop_id in C:\MAMP\htdocs\wp50\wp-content\plugins\tm-gtfs-data\tm-gtfs-data.php on line 453
11
Notice: Undefined index: from_stop_id in C:\MAMP\htdocs\wp50\wp-content\plugins\tm-gtfs-data\tm-gtfs-data.php on line 453
31
Notice: Undefined index: from_stop_id in C:\MAMP\htdocs\wp50\wp-content\plugins\tm-gtfs-data\tm-gtfs-data.php on line 453
41

I cannot understand WHY it is giving me an undefined index when I know the following. $transfers_csv is AN Numeric Indexed Array. I can see from the output that there are values for 'from_stop_id', for 'to_stop_id' and 'transfer_type' but 'min_transfer_type' value is empty or NULL.

Anyone see what I am doing wrong here??

5
  • 6
    Are you sure key 11, 31 and 41 exist? Why not make it simple and use foreach? here is a portion of the print_r expand that example to past key 12. Commented Oct 29, 2019 at 17:14
  • 2
    did you try var_dumping $transfers_csv[$counter] to ensure it's holding the data you're expecting? Commented Oct 29, 2019 at 17:14
  • Possible duplicate of "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP Commented Oct 29, 2019 at 17:15
  • @trey you mean $transfers_csv? $counter is an integer of the for loop Commented Oct 29, 2019 at 17:15
  • 1
    @Andreas nah, as $transfers_csv[$counter] should show an array with the key == counter - though, as you've said, it would be easier (for everyone) if a foreach was used here^^ Commented Oct 29, 2019 at 17:18

1 Answer 1

4

You are obviously reading the headers from a csv file and mistakenly including the the BOM (Byte Order Mark) in the first header from_stop_id.

So from_stop_id is actually \uFEFFfrom_stop_id, that's why PHP is throwing the an undefined index notice.

Read more about it here: https://en.wikipedia.org/wiki/Byte_order_mark

Not sure if you're using fgetscsv() but it's known that this function have problems when dealing with the BOM.

If you have control over the csv file you can save it without including the BOM.

It's possible to deal with this on your code alos, one way would be something like:

$fp = fopen($path, 'r');

if (fgets($fp, 4) !== "\xef\xbb\xbf") {
    rewind($fp);
}

// call fgetcsv() as usual
Sign up to request clarification or add additional context in comments.

Comments

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.