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
)
)
CodePudding user response:
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.