如何解决PHP数组,使日期键然后合并相同的日期?
我的数组(存储在$ r中)当前看起来是这样的……(此示例中每天有3个条目具有相同的日期,有时会有更多的条目共享相同的日期。)
Array
(
[0] => Array
(
[id] => 1
[date_inserted] => 2020-09-06
[marketplace] => Etsy
[total_stickers] => 11
[total_orders] => 8
[total_value] => 41.62
[1] => Array
(
[id] => 2
[date_inserted] => 2020-09-06
[marketplace] => Woo
[total_stickers] => 2
[total_orders] => 1
[total_value] => 7.98
)
[2] => Array
(
[id] => 3
[date_inserted] => 2020-09-06
[marketplace] => eBay
[total_stickers] => 44
[total_orders] => 40
[total_value] => 115.63
)
[3] => Array
(
[id] => 4
[date_inserted] => 2020-09-07
[marketplace] => Etsy
[total_stickers] => 8
[total_orders] => 4
[total_value] => 34.92
)
[4] => Array
(
[id] => 5
[date_inserted] => 2020-09-07
[marketplace] => Woo
[total_stickers] => 9
[total_orders] => 3
[total_value] => 52.90
)
[5] => Array
(
[id] => 6
[date_inserted] => 2020-09-07
[marketplace] => eBay
[total_stickers] => 23
[total_orders] => 21
[total_value] => 58.03
)
)
如果将日期作为键并将每个日期的详细信息合并在一起,则我认为这样做会更好,也不需要[id],并且“市场”也可能是内部数组的键,因此最终输出我想是这样。
Array
(
[2020-09-06] => Array (
[Etsy] => Array
(
[total_stickers] => 11
[total_orders] => 8
[total_value] => 41.62
)
[Woo] => Array
(
[total_stickers] => 2
[total_orders] => 1
[total_value] => 7.98
)
[eBay] => Array
(
[total_stickers] => 44
[total_orders] => 40
[total_value] => 115.63
)
)
[2020-09-07] => Array (
[Etsy] => Array
(
[total_stickers] => 8
[total_orders] => 4
[total_value] => 34.92
)
[Woo] => Array
(
[total_stickers] => 9
[total_orders] => 3
[total_value] => 52.90
)
[eBay] => Array
(
[total_stickers] => 23
[total_orders] => 21
[total_value] => 58.03
)
)
)
我一直在看array_merge_recursive,也在这里看array_combine以及类似的问题,但似乎无法弄清楚如何获得我想要的结果。
这是我尝试过的方法(基于劳伦斯·谢罗内的回答)
$results = [];
foreach (array_column($r,'date_inserted') as $bydate) {
foreach ($r as $item) {
$results[$bydate][$item['marketplace']] = [
'total_orders' => $item['total_orders'],'total_stickers' => $item['total_stickers'],'total_value' => $item['total_value']
];
}
}
但是结果是,尽管结构现在看起来正确,但每天的数据本身都是相同的(不使用当天的数据)。
这是结果数组
Array
(
[2020-09-06] => Array
(
[Etsy] => Array
(
[total_orders] => 2
[total_stickers] => 3
[total_value] => 7.83
)
[Woo] => Array
(
[total_orders] => 10
[total_stickers] => 20
[total_value] => 100.38
)
[eBay] => Array
(
[total_orders] => 17
[total_stickers] => 18
[total_value] => 67.36
)
)
[2020-09-07] => Array
(
[Etsy] => Array
(
[total_orders] => 2
[total_stickers] => 3
[total_value] => 7.83
)
[Woo] => Array
(
[total_orders] => 10
[total_stickers] => 20
[total_value] => 100.38
)
[eBay] => Array
(
[total_orders] => 17
[total_stickers] => 18
[total_value] => 67.36
)
)
)
解决方法
尝试此操作,使用array_column挑选日期,在日期上循环,然后使用array_filter仅过滤带有日期的项目,然后使用市场作为关键字将项目添加到数组中。
<?php
$data = [
['id' => 1,'date_inserted' => '2020-09-06','marketplace' => 'Etsy','total_stickers' => 11,'total_orders' => 8,'total_value' => 41.619999999999997],['id' => 2,'marketplace' => 'Woo','total_stickers' => 2,'total_orders' => 1,'total_value' => 7.9800000000000004],['id' => 3,'marketplace' => 'eBay','total_stickers' => 44,'total_orders' => 40,'total_value' => 115.63],['id' => 4,'date_inserted' => '2020-09-07','total_stickers' => 8,'total_orders' => 4,'total_value' => 34.920000000000002],['id' => 5,'total_stickers' => 9,'total_orders' => 3,'total_value' => '52.90'],['id' => 6,'total_stickers' => 23,'total_orders' => 21,'total_value' => 58.030000000000001]
];
$result = [];
foreach (array_column($data,'date_inserted') as $date) {
foreach (array_filter($data,function($v) use ($date) {
return $v['date_inserted'] === $date;
}) as $item) {
$result[$date][$item['marketplace']] = [
'id' => $item['id'],'total_stickers' => $item['total_stickers'],'total_orders' => $item['total_orders'],'total_value' => $item['total_value']
];
}
}
print_r($result);
结果:
Array
(
[2020-09-06] => Array
(
[Etsy] => Array
(
[id] => 1
[total_stickers] => 11
[total_orders] => 8
[total_value] => 41.62
)
[Woo] => Array
(
[id] => 2
[total_stickers] => 2
[total_orders] => 1
[total_value] => 7.98
)
[eBay] => Array
(
[id] => 3
[total_stickers] => 44
[total_orders] => 40
[total_value] => 115.63
)
)
[2020-09-07] => Array
(
[Etsy] => Array
(
[id] => 4
[total_stickers] => 8
[total_orders] => 4
[total_value] => 34.92
)
[Woo] => Array
(
[id] => 5
[total_stickers] => 9
[total_orders] => 3
[total_value] => 52.90
)
[eBay] => Array
(
[id] => 6
[total_stickers] => 23
[total_orders] => 21
[total_value] => 58.03
)
)
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。