Pergunta

I'm converting one array into another array by changing some key names, by creating new array keys, etc. In the code I tried I'm getting following warning multiple times(i.e. the indication that loop is executing) and a blank array.

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

The array titled $form_data on which I'm doing the operation is as follows:

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
        )

)

The code I tried to manipulate the above array is as below. In form of comments I've mentioned the line nos. for which I'm getting warning. This is for your better readability and understanding my issue quicker.

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

If you want any further information regarding my question I can provide you the same. Thanks in advance.

Foi útil?

Solução

You can't use an array as the index in another array:

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

You probably meant to use $index instead:

    $rebate_product[$index][$key][$k]['state_id'] = $v;//Line No.117
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top