Php 命令行编程时出错
当我尝试使用命令行处理文件时:Php 命令行编程时出错,php,command-line,Php,Command Line,当我尝试使用命令行处理文件时: 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\
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);
在foreach循环中,您正在访问数组值,而不检查它们是否存在 每次访问不存在的索引时,PHP都会给您一个通知——在您的例子中,是堆栈跟踪(我假设它是XDebug)。一个通知不是一个错误,你的脚本只是继续,并得到另一个通知,另一个,等等 您可以使用
isset($order[$index])
检查索引是否存在
此外,您的应用程序似乎正在读取CSV数据。如何使用php的内置程序来处理许多您的代码无法处理的边缘情况
顺便说一句,您的
foreach
循环是不必要的,您可以在读取记录后在while循环中进行处理。在您的foreach循环中,您访问数组值而不检查它们是否存在
每次访问不存在的索引时,PHP都会给您一个通知——在您的例子中,是堆栈跟踪(我假设它是XDebug)。一个通知不是一个错误,你的脚本只是继续,并得到另一个通知,另一个,等等
您可以使用isset($order[$index])
检查索引是否存在
此外,您的应用程序似乎正在读取CSV数据。如何使用php的内置程序来处理许多您的代码无法处理的边缘情况
顺便说一下,您的
foreach
循环是不必要的,您可以在读取记录后在while循环中进行处理。index.php中的第92-104行是什么?您正在访问不存在的数组项。我已更新了代码,代码第92行到第104行有一个片段,我不知道,但它在一小时前运行良好,我没有更改任何内容。@Malek您在fgets()
调用中丢失了trim()
-我猜您得到了一些空项<代码>var_转储($all_订单)待确认;-)index.php第92-104行有什么内容?您正在访问不存在的数组项。我已更新了代码,代码第92行到第104行有一个片段,我不知道,但它在一小时前运行良好,我没有更改任何内容。@Malek您在fgets()
调用中丢失了trim()
-我猜您得到了一些空项<代码>var_转储($all_订单)待确认;-)我已经建议了fgetcsv()
,但是OP似乎对这个特定文件的行结束有问题,这导致fgets()
为每个其他记录返回空行。他在我之前给他的一些代码中缺少了一个trim()
,这可能会再次给他空记录-。我已经建议过fgetcsv()
,但是OP似乎在这个特定文件的行尾有问题,导致fgets()
为其他记录返回空行。他在我之前给他的一些代码中缺少了一个trim()
,这些代码可能又给了他空记录-。