当我尝试使用命令行处理文件时:
cat texte.txt|php index.php我有一堆奇怪的东西:
C:\wamp\www\Fraud Prevention>cat texte.txt|php index.php
PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on li
ne 92
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 1 in C:\wamp\www\Fraud Prevention\index.php on line 92
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on li
ne 94
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 2 in C:\wamp\www\Fraud Prevention\index.php on line 94
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on li
ne 96
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 3 in C:\wamp\www\Fraud Prevention\index.php on line 96
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on li
ne 98
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 4 in C:\wamp\www\Fraud Prevention\index.php on line 98
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on li
ne 100
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 5 in C:\wamp\www\Fraud Prevention\index.php on line 10
0
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on li
ne 102
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 6 in C:\wamp\www\Fraud Prevention\index.php on line 10
2
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
PHP Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on li
ne 104
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0
Notice: Undefined offset: 7 in C:\wamp\www\Fraud Prevention\index.php on line 10
4
Call Stack:
0.0005 330608 1. {main}() C:\wamp\www\Fraud Prevention\index.php:0编辑:
下面是我在92行和104行之间的代码:
$handle = fopen ("php://stdin","r");
$N = $title = fgets($handle, 1024);
while(!feof($handle))
{
if (!$order = fgets($handle)) continue;
$all_orders[] = explode(',', $order);
}
foreach ($all_orders as &$order){
$order["Order id"]=$order[0];
unset($order[0]);
$order["Deal id"]=$order[1];
unset($order[1]);
$order["Email address"]=$order[2];
unset($order[2]);
$order["Street address"]=$order[3];
unset($order[3]);
$order["City"]=$order[4];
unset($order[4]);
$order["State"]=$order[5];
unset($order[5]);
$order["Zip Code"]=$order[6];
unset($order[6]);
$order["Credit Card"]=$order[7];
unset($order[7]);
}
print_r($all_orders);发布于 2012-01-08 16:22:50
在foreach循环中,您访问数组值而不检查它们是否存在。
每次访问一个不存在的索引时,PHP都会给您一个通知--在您的例子中,是一个堆栈跟踪(我假设它是XDebug)。一个通知不是一个炫耀的错误,您的脚本只是继续下去,然后得到另一个通知,等等。
您可以使用isset($order[$index])检查索引是否存在。
另外,你似乎在读CSV的数据。如何使用php的内置fgetcsv(),它已经处理了许多您的代码没有处理的边缘情况?
顺便说一句,您的foreach循环是不相关的,在读取记录之后,您可以在while循环中执行该处理。
https://stackoverflow.com/questions/8779026
复制相似问题