【发布时间】:2020-02-24 23:37:29
【问题描述】:
我有一个数字索引数组,这里是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] =>
)
)
这是我的 php 循环:
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'];
}
这是我的错误输出:
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
当我知道以下内容时,我无法理解为什么它会给我一个未定义的索引。 $transfers_csv 是一个数字索引数组。 我可以从输出中看到“from_stop_id”、“to_stop_id”和“transfer_type”的值,但“min_transfer_type”的值为空或 NULL。
有人看到我在这里做错了吗??
【问题讨论】:
-
您确定密钥 11、31 和 41 存在吗?为什么不让它变得简单并使用foreach?
here is a portion of the print_r将该示例扩展到过去的键 12。 -
您是否尝试过 var_dumping
$transfers_csv[$counter]以确保它保存您期望的数据? -
@trey 你的意思是
$transfers_csv? $counter 是 for 循环的整数 -
@Andreas nah,
$transfers_csv[$counter]应该显示一个带有键 == 计数器的数组 - 不过,正如您所说,如果在这里使用 foreach 会更容易(对于每个人)^ ^