问题代码:

$accountSales = (new OrderSkuModel())
            ->setTable($this->getTableName($params))
            ->field($fields)
            ->where($where)
            ->select();
            
            foreach ($accountSales as $reportOrder) {
                if (isset($localAccountSysOrder[$reportOrder['code']])) {
                    $reportOrder['profit'] += $localAccountSysOrder[$reportOrder['code']]['profit'];
                    $reportOrder['actual_amount'] += $localAccountSysOrder[$reportOrder['code']]['actual_amount'];
                    $reportOrder['order_qty'] += $localAccountSysOrder[$reportOrder['code']]['order_qty'];
                    $reportOrder['distribution_details']['fba_warehouse_amount'] += $localAccountSysOrder[$reportOrder['code']]['distribution_details']['fba_warehouse_amount'];
                    $reportOrder['distribution_details']['fba_warehouse_orders'] += $localAccountSysOrder[$reportOrder['code']]['distribution_details']['fba_warehouse_orders'];                   
                }
            $accountSalesProfitMerge[] = $reportOrder;
        }

12行的代码赋值会报错“Indirect modification of overloaded element of *** has no effect”

分析

我们可以var_dump一下 $accountSales 查看一下:
···
object(think\Collection)[16]
protected ‘items’ =>
array (size=3)

···
这明显是个对象,应该是能够遍历但是不能直接为其赋值。那么问题就比较好解决了。

解决方案

  1. 将对结果集再次处理转换一下:
$accountSales = collection($accountSales)->toArray();
  1. 给一个中间变量
$fbaWarehouseAmount = $fbaOrderReport['distribution_details']['fba_warehouse_amount'] + $fbaAccountChannelOrder[$fbaOrderReport['seller_id']]['distribution_details']['fba_warehouse_amount'];
$fbaWarehouseOrders = $fbaOrderReport['distribution_details']['fba_warehouse_orders'] + $fbaAccountChannelOrder[$fbaOrderReport['seller_id']]['distribution_details']['fba_warehouse_orders'];

$temp1 = [];
$temp1['fba_warehouse_amount'] = $fbaWarehouseAmount;
$temp1['fba_warehouse_orders'] = $fbaWarehouseOrders;
$fbaAccountReport[$key]['distribution_details'] = $temp1;

尝试了两种方法都可解决~