Reputation: 13
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??
Upvotes: 0
Views: 289
Reputation: 2194
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
Upvotes: 4