首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命令行编程时的错误

命令行编程时的错误
EN

Stack Overflow用户
提问于 2012-01-08 16:07:40
回答 1查看 882关注 0票数 0

当我尝试使用命令行处理文件时:

代码语言:javascript
复制
cat texte.txt|php index.php

我有一堆奇怪的东西:

代码语言:javascript
复制
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行之间的代码:

代码语言:javascript
复制
    $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);
EN

回答 1

Stack Overflow用户

发布于 2012-01-08 16:22:50

在foreach循环中,您访问数组值而不检查它们是否存在。

每次访问一个不存在的索引时,PHP都会给您一个通知--在您的例子中,是一个堆栈跟踪(我假设它是XDebug)。一个通知不是一个炫耀的错误,您的脚本只是继续下去,然后得到另一个通知,等等。

您可以使用isset($order[$index])检查索引是否存在。

另外,你似乎在读CSV的数据。如何使用php的内置fgetcsv(),它已经处理了许多您的代码没有处理的边缘情况?

顺便说一句,您的foreach循环是不相关的,在读取记录之后,您可以在while循环中执行该处理。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8779026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档