具有相同ID的阵列合并产品

时间:2019-11-14 17:59:07

标签: php arrays laravel

我对此进行了编码,以得到具有所有产品顺序的数组

$sync_data = [];
    for ($i = 0; $i < count($request['product_id']); $i++) {
        $sync_data[$request->input('product_id')[$i]] = [
            'sequence_no' => $i+1,
            'type' => 'order',
            'quantity' => $request->input('quantity')[$i],
            'listprice' => $request->input('listprice')[$i],
            'discount_percent' => NULL,
            'discount_amount' => $request->input('discount_amount')[$i],
            'comment' => $request->input('comment')[$i],
            'description' => '',
            'tax1' => $request->input('tax1')[$i],
            'tax2' => $request->input('tax2')[$i],
            'tax3' => NULL,
            'tax4' => NULL,
            'campaign_id' => $request->input('campaign_id')[$i]
        ];            
    }

但是它正在合并具有相同ID的产品,所以我得到了

array:2 [▼
  194 => array:13 [▶]
  191 => array:13 [▶]
]

我想要这个

array:2 [▼
  194 => array:13 [▶]
  194 => array:13 [▶]
  191 => array:13 [▶]
]

有人可以向我解释我在想什么吗?

预先感谢=)

1 个答案:

答案 0 :(得分:0)

找到它=)谢谢大家的帮助

for ($i = 0; $i < count($request['product_id']); $i++) {
            $sync_data[] = [
                'sequence_no' => $i+1,
                'type' => 'order',
                'quantity' => $request->input('quantity')[$i],
                'listprice' => $request->input('listprice')[$i],
                'discount_percent' => NULL,
                'discount_amount' => $request->input('discount_amount')[$i],
                'comment' => $request->input('comment')[$i],
                'description' => '',
                'tax1' => $request->input('tax1')[$i],
                'tax2' => $request->input('tax2')[$i],
                'tax3' => NULL,
                'tax4' => NULL,
                'product_id' => $request->input('product_id')[$i],
                'campaign_id' => $request->input('campaign_id')[$i]
            ];    

        }
        foreach ($sync_data as $product) {            
            $order->products()->attach($product['product_id'], $product);
        }