0

I am inserting 3 dimension array, therefore trying to use a nested for each loop. But I am kept getting Error : Illegal string offset 'product_id'

My codes are :

foreach($request->transfer as $key => $value){
            
    foreach($request->transfer[$key]['product'] as $n_key => $n_value){

        $storage_product = StorageProduct::create([
            'storage_id'   => $request->transfer[$key]['storage_id'],
            'product_id'   => $request->transfer[$key]['product'][$n_key]['product_id'],
            'transfer_id'  => $supply_to_storage->id,
            'quantity'     => $request->transfer[$key]['product'][$n_key]['quantity']
        ]);
    }
}

The array looks like : I am adding some dummy text since it's notifying me "mostly code". Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

 Array
 (
  [0] => Array
      (
          [product] => Array
              (
                [0] => Array
                    (
                        [product_id] => 1
                        [quantity] => 12
                        [total_quantity] => 12
                        [rate] => 14
                    )

                [product_varient_id] => 2
                [1] => Array
                    (
                        [product_id] => 2
                        [quantity] => 4
                        [total_quantity] => 12
                        [rate] => 11
                    )

            )

        [transfer_number] => 
        [storage_id] => 2
        [storage_product_quantity] => 12
        [status] => 8
    )

[1] => Array
    (
        [product] => Array
            (
                [0] => Array
                    (
                        [product_id] => 1
                        [quantity] => 11
                        [total_quantity] => 12
                        [rate] => 5
                    )

                [product_varient_id] => 1
            )

        [transfer_number] => 
        [storage_id] => 1
        [storage_product_quantity] => 12
        [status] => 8
    )

 )

enter image description here

K.S. Azim
  • 127
  • 1
  • 12
  • It's easier if you post the array as text instead of an image. – M. Eriksson Apr 14 '22 at 01:00
  • You get that error because you have an `[product_variant_id]` in the list you're iterating through. So this: `$request->transfer[$key]['product'][$n_key]['product_id']` will be the same as `$request->transfer[$key]['product']['product_variant_id']['product_id']`. So your code is trying to get the key `'product_id'` from the value `2`. It's a very strange array structure. Where does it come from? – M. Eriksson Apr 14 '22 at 01:03
  • thanks for the help, it's coming from a form lol (inventory system) – K.S. Azim Apr 14 '22 at 01:10

1 Answers1

0

The problem is product varient id key. It has same indentation with 0 and 1 array keys which they have product id inside them. However, product_variant_id key does not have product_key in it.

gguney
  • 2,512
  • 1
  • 12
  • 26