为什么我收到警告“警告:abc.php中的非法偏移类型”?

时间:2014-05-08 06:15:03

标签: php arrays multidimensional-array associative-array key-value

我通过更改一些键名,通过创建新的数组键等将一个数组转换为另一个数组。在我尝试的代码中,我多次得到以下警告(即循环的指示是执行)和一个空白数组。

Warning: Illegal offset type in /var/www/smart-rebate-web/model/RebateByProduct.php on line 117
Warning: Illegal offset type in /var/www/smart-rebate-web/model/RebateByProduct.php on line 120
Warning: Illegal offset type in /var/www/smart-rebate-web/model/RebateByProduct.php on line 130
Warning: Illegal offset type in /var/www/smart-rebate-web/model/RebateByProduct.php on line 138

我正在进行操作的标题为$form_data的数组如下:

Array
(
    [form_submitted] => yes
    [op] => add
    [company_id] => 46
    [1] => Array
        (
            [products] => Array
                (
                    [1] => 8
                    [2] => 11
                )

            [pack] => 10
            [quantity] => 20
            [volume] => 30
            [units] => 9
            [amount] => 40
            [rebate_start_date] => 2014-05-01
            [rebate_expiry_date] => 2014-05-10
            [applicable_states] => Array
                (
                    [0] => 1
                    [1] => 4
                    [2] => 6
                    [3] => 8
                )

            [rebate_total_count] => 1000
        )

    [2] => Array
        (
            [products] => Array
                (
                    [1] => 10
                    [2] => 9
                )

            [pack] => 50
            [quantity] => 60
            [volume] => 70
            [units] => 10
            [amount] => 80
            [rebate_start_date] => 2014-05-21
            [rebate_expiry_date] => 2014-05-30
            [applicable_states] => Array
                (
                    [0] => 13
                    [1] => 22
                    [2] => 29
                    [3] => 39
                    [4] => 44
                )

            [rebate_total_count] => 5000
        )

)

我试图操纵上面的数组的代码如下。以评论的形式,我提到了行号。为此我收到了警告。这是为了您更好的可读性和更快地理解我的问题。

      $rebate_product = array();
      $arr_key = array('pack', 'quantity', 'volume', 'units', 'amount');

      foreach ($form_data as $index => $element) { 
        if(is_array($element)) {
          foreach($element as $key => $value) {
            if (is_array($value)) {
              foreach ($value as $k => $v) {
                if ($key == 'applicable_states')                    
                  $rebate_product[$element][$key][$k]['state_id'] = $v;//Line No.117

                if ($key == 'products')
                  $rebate_product[$element][$key][$k-1]['product_id'] = $v;//Line No.120                    
              }

            } else {
              if (in_array($key, $arr_key)) {
                if ($key == 'pack')
                  $key = 'pack_of';
                if ($key == 'units')
                  $key = 'volume_unit_id';

                $rebate_product[$element]['saleable_unit'][$key] = $value;//Line No. 130              
              } else {
                if ($key == 'rebate_total_count')
                  $key = 'count';
                if ($key == 'rebate_start_date')
                  $key = 'start_date';
                if ($key == 'rebate_expiry_date')
                  $key = 'end_date';
                $rebate_product[$element][$key] = $value;//Line No. 138           
              }            
            }
            /*if (!array_key_exists('applicable_states', $rebate_product[$element])) {
              $rebate_product[$element]['applicable_states'] = array();
            }*/
          }    
        }
      }

如果您想了解有关我的问题的任何进一步信息,我可以为您提供相同的信息。提前谢谢。

1 个答案:

答案 0 :(得分:2)

您不能将数组用作另一个数组中的索引:

if(is_array($element)) {
    // <...snip...>
    $rebate_product[$element][$key][$k]['state_id'] = $v;//Line No.117
    //              ^^^^^^^^

您可能打算使用$index代替:

    $rebate_product[$index][$key][$k]['state_id'] = $v;//Line No.117