【问题标题】:PHP Array Notice undefined index - Its defined?PHP Array 注意未定义的索引 - 它已定义?
【发布时间】:2014-11-17 13:59:48
【问题描述】:

我无法理解我的意思是应该可以正常工作的代码。

代码如下:

        foreach( $_POST['wageline'] AS $line ){
            print_r( $line );
            if( !$MySQL->Query(" 
                INSERT INTO
                    wages_lines
                SET
                    'batch_id' = '" . $this->batch_id . "',
                    'name' = '" . $line['name'] . "',
                    'identity_number' = " . $line['identity_number'] . "',
                    'account' = '" . $line['account'] . "',
                    'phone' = '" . $line['phone'] . "',
                    'ordinary_hours' = '" . $line['ordinary_hours'] . "',
                    'fifty_percent' = '" . $line['fifty_percent'] . "',
                    'hundred_percent' = '" . $line['hundred_percent'] . "',
                    'mileage' = '" . $line['mileage'] . "',
                    'remuneration' = '" . $line['remuneration'] . "',
                    'pall_dag_2' = '" . $line['pall_dag_2'] . "',
                    'pall_dag_3' = '" . $line['pall_dag_3'] . "',
                    'pall_natt_2' = '" . $line['pall_natt_2'] . "',
                    'pall_natt_3' = '" . $line['pall_natt_3'] . "',
                    'ub_1' = '" . $line['ub_1'] . "',
                    'ub_2' = '" . $line['ub_2'] . "',
                    'ub_3' = '" . $line['ub_3'] . "',
                    'ub_4' = '" . $line['ub_4'] . "',
                    'ub_5' = '" . $line['ub_5'] . "',
                    'employee_wage' = '" . $line['employee_wage'] . "',
                    'brutto_total' = '" . $line['brutto_total'] . "',
                    'mileage_total' = '" . $line['mileage_total'] . "',
                    'remuneration_total' = '" . $line['remunerations_total'] . "'
            ") )

我的 print_r 的结果如下:

Array
(
['name'] => Alexander Fagerstrand
['identity_number'] => 
['account'] => 
['phone'] => 0
['ordinary_hours'] => 32.5
['fifty_percent'] => 0
['hundred_percent'] => 0
['mileage'] => 458
['remuneration'] => 628
['pall_dag_2'] => 0
['pall_dag_3'] => 0
['pall_natt_2'] => 0
['pall_natt_3'] => 0
['ub_1'] => 0
['ub_2'] => 0
['ub_3'] => 0
['ub_4'] => 0
['ub_5'] => 0
['employee_wage'] => 177
['brutto_total'] => 5752.50
['mileage_total'] => 1854.9
['remunerations_total'] => 628
)

不过,我还是收到了这样的通知:


通知:未定义索引:/var/www/vhosts/ontimebemanning.no/portal.ontimebemanning.no/modules/core.wages/classes/wage 中的名称。第 573 行的 class.php

通知:未定义索引:/var/www/vhosts/ontimebemanning.no 中的标识号/portal.ontimebemanning.no/modules/core.wages/classes/wage.class.php574

显然,它们是已定义的,所以我不知道为什么我的脚本会向我抛出通知。 有人知道吗?

【问题讨论】:

  • ['identity_number'] => ['account'] => 他们对我来说似乎没有定义
  • @MarcoMura 值似乎未定义,但键仍然存在
  • @MarcoMura 它们已定义但未分配值
  • 值是未定义的,是的,但索引不是。它还会在包含值的索引上抛出未定义索引错误
  • 当我使用 print_r 时,我看不到引号。我看到类似 [key] => value 的东西。因为您看到的是引号,所以它告诉我您在键值中嵌入了引号 - 或者您手动输入并添加了引号。如果引号在键值中,如图所示,不定义不带引号的键。

标签: php arrays


【解决方案1】:

如果这是print_r() 的输出,则在 POST 请求中发送的数据在键名周围有引号;现在要访问数据,您必须这样做:

echo $lines["'name'"];

或者,更优选地,找出为什么转移这些额外的引号并将它们删除。

【讨论】:

  • 谢谢,确实是这样=)
【解决方案2】:

您的代码中有错误。您在查询代码中使用 => 而不是 =

【讨论】:

  • 但是,未定义索引的问题仍然存在
  • 如果问题中存在不直接解决实际问题或与问题无关的问题,请不要发布解决方案作为答案,而是发表评论。问题的答案应该回答那个问题,而不是你发现的可能有远程有用的随机事物。
猜你喜欢
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 2017-02-02
  • 2021-03-07
相关资源
最近更新 更多