【问题标题】:Why I'm getting a warning as "Warning: Illegal offset type in abc.php"?为什么我收到警告为“警告:abc.php 中的非法偏移类型”?
【发布时间】:2014-06-25 09:23:56
【问题描述】:

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

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
        )

)

我试图操作上述数组的代码如下。以 cmets 的形式,我提到了行号。我收到警告。这是为了让您更好地阅读并更快地理解我的问题。

      $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();
            }*/
          }    
        }
      }

如果您想了解有关我的问题的更多信息,我可以为您提供同样的信息。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    你不能使用一个数组作为另一个数组的索引:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2021-09-05
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多