我有一个数组如下。 atrributes是每个数组的order_id,qty,sell_price,product_name和merchant_name。我想基于相同的order_id进行映射,如果相同的order_id大于1,则将属性更改为数组。
[items:protected] => Array
(
[0] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 45000.00
[product_name] => Omni Bars Cokelat Kelapa
[merchant_name] => Health & Co.
)
[1] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 55000.00
[product_name] => Trim Eats Strawberry Banana Cream Overnight Oats
[merchant_name] => Trim Eats
)
[2] => stdClass Object
(
[order_id] => 1
[qty] => 1
[sell_price] => 28000.00
[product_name] => Everything Banana Keripik Pisang Rumput Laut
[merchant_name] => EverythingBanana
)
[3] => stdClass Object
(
[order_id] => 2
[qty] => 1
[sell_price] => 35000.00
[product_name] => Naked Bar Banana Nutter
[merchant_name] => Naked Foodss
)
[4] => stdClass Object
(
[order_id] => 3
[qty] => 1
[sell_price] => 200000.00
[product_name] => Health & Co. Paket Detoks 1 Hari
[merchant_name] => Health & Co.
)
[5] => stdClass Object
(
[order_id] => 3
[qty] => 1
[sell_price] => 38000.00
[product_name] => Health & Co. Paket 3 Botol Holy Kale
[merchant_name] => Health & Co.
)
)
我想将数组映射到类似的东西,键是order_id。
[1] => stdClass Object
(
[qty] => [1,1,1]
[sell_price] => [45000.00, 55000.00, 28000.00]
[product_name] => [Omni Bars Cokelat Kelapa, Trim Eats Strawberry Banana Cream Overnight Oats, Everything Banana Keripik Pisang Rumput Laut]
[merchant_name] => [Health & Co, Trim Eats, EverythingBanana]
)
[2] => so on....
我坚持下去。请帮帮我。
答案 0 :(得分:0)
看看这个解决方案:
//$data - your array
$result = [];
foreach ($data as $item) {
$result[$item['order_id']]['qty'][] = $item['qty'];
$result[$item['order_id']]['sell_price'][] = $item['sell_price'];
$result[$item['order_id']]['product_name'][] = $item['product_name'];
$result[$item['order_id']]['merchant_name'][] = $item['merchant_name'];
}
// $result your result array