php - 以数组值作为键的数组合并
问题描述
我正在尝试转换产品数据的多维数组,其中将 PayPal 电子邮件设置为数组值之一(某些值可能相同)。我需要帮助将它们合并为 paypal_email 键相同的数组。这是print_r
我拥有的数组。
Array
(
[0] => Array
(
[name] => Test Product
[product_id] => 307
[variation_id] => 0
[subtotal] => 30
[total] => 30
[quantity] => 2
[subtotal_tax] => 2.66
[total_tax] => 2.66
[paypal_email] => thisismytestpaypal@sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Album
[product_id] => 354
[variation_id] => 0
[subtotal] => 15
[total] => 15
[quantity] => 1
[subtotal_tax] => 1.33
[total_tax] => 1.33
[paypal_email] => thisismytestpaypal@sandbox.com
[vendor_id] => 11
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[2] => Array
(
[name] => Beanie with Logo
[product_id] => 357
[variation_id] => 0
[subtotal] => 18
[total] => 18
[quantity] => 1
[subtotal_tax] => 1.60
[total_tax] => 1.60
[paypal_email] => ab2@sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[3] => Array
(
[name] => Long Sleeve Tee
[product_id] => 352
[variation_id] => 0
[subtotal] => 75
[total] => 75
[quantity] => 3
[subtotal_tax] => 6.66
[total_tax] => 6.66
[paypal_email] => ab2@sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[4] => Array
(
[name] => Polo
[product_id] => 353
[variation_id] => 0
[subtotal] => 80
[total] => 80
[quantity] => 4
[subtotal_tax] => 7.10
[total_tax] => 7.10
[paypal_email] => tv3@sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[5] => Array
(
[name] => Product From Test Vendor 3
[product_id] => 145
[variation_id] => 0
[subtotal] => 60
[total] => 60
[quantity] => 3
[subtotal_tax] => 5.33
[total_tax] => 5.33
[paypal_email] => tv3@sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[6] => Array
(
[name] => Product From Vendor 1
[product_id] => 143
[variation_id] => 0
[subtotal] => 20
[total] => 20
[quantity] => 2
[subtotal_tax] => 1.78
[total_tax] => 1.78
[paypal_email] => tv1@sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
我需要将数组合并为一个多维数组,其中贝宝电子邮件作为键,其他数据作为值。例如,
Array
( [thisismytestpaypal@sandbox.com] => Array
(
[0] => Array
(
[name] => Test Product
[product_id] => 307
[variation_id] => 0
[subtotal] => 30
[total] => 30
[quantity] => 2
[subtotal_tax] => 2.66
[total_tax] => 2.66
[paypal_email] => thisismytestpaypal@sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Album
[product_id] => 354
[variation_id] => 0
[subtotal] => 15
[total] => 15
[quantity] => 1
[subtotal_tax] => 1.33
[total_tax] => 1.33
[paypal_email] => thisismytestpaypal@sandbox.com
[vendor_id] => 11
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[ab2@sandbox.com] => Array
(
[0] => Array
(
[name] => Beanie with Logo
[product_id] => 357
[variation_id] => 0
[subtotal] => 18
[total] => 18
[quantity] => 1
[subtotal_tax] => 1.60
[total_tax] => 1.60
[paypal_email] => ab2@sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Long Sleeve Tee
[product_id] => 352
[variation_id] => 0
[subtotal] => 75
[total] => 75
[quantity] => 3
[subtotal_tax] => 6.66
[total_tax] => 6.66
[paypal_email] => ab2@sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[tv3@sandbox.com] => Array
(
[0] => Array
(
[name] => Polo
[product_id] => 353
[variation_id] => 0
[subtotal] => 80
[total] => 80
[quantity] => 4
[subtotal_tax] => 7.10
[total_tax] => 7.10
[paypal_email] => tv3@sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Product From Test Vendor 3
[product_id] => 145
[variation_id] => 0
[subtotal] => 60
[total] => 60
[quantity] => 3
[subtotal_tax] => 5.33
[total_tax] => 5.33
[paypal_email] => tv3@sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[tv1@sandbox.com] => Array
(
[0] => Array
(
[name] => Product From Vendor 1
[product_id] => 143
[variation_id] => 0
[subtotal] => 20
[total] => 20
[quantity] => 2
[subtotal_tax] => 1.78
[total_tax] => 1.78
[paypal_email] => tv1@sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
)
我怎样才能做到这一点?
谢谢
解决方案
$orders = [.....]; //your array
$mappedOrders = [];
foreach($orders as $order) {
$mail = $order['paypal_email'];
if(!isset($mappedOrders[$mail])) { $mappedOrders[$mail] = []; } //create new array in map if $mail index not exists
$mappedOrders[$mail][] = $order; // push order into array for mapped array
}
var_dump($mappedOrders);
推荐阅读
- java - JLink 不生成可再分发的图像
- assembly - 什么是 JKZD 和 JKNZD?
- java - RecyclerView 在对象删除时更新视图,最后一个对象除外
- go - 删除转换更改语义
- sql - 执行内部联接时 sql 中的预期行数
- oracle - 从 Oracle 到 Spark sql 的查询转换
- flutter - 如何在另一个具有大量参数的 StatefulWidget 类中使用自定义文本字段?
- snowflake-cloud-data-platform - 用于创建表的雪花赠款
- c++ - 当您首先绘制具有相似纹理的精灵时,sf::RenderWindow 绘制速度是否更快
- c++ - 编码接受来自未指定来源的随机位的加密算法