php - 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 以及类似的问题,但似乎无法弄清楚如何获得我想要的结果。
这就是我尝试过的(基于 Lawrence Cherone 的回答)
$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, 'date_inserted' => '2020-09-06', 'marketplace' => 'Woo', 'total_stickers' => 2, 'total_orders' => 1, 'total_value' => 7.9800000000000004],
['id' => 3, 'date_inserted' => '2020-09-06', 'marketplace' => 'eBay', 'total_stickers' => 44, 'total_orders' => 40, 'total_value' => 115.63],
['id' => 4, 'date_inserted' => '2020-09-07', 'marketplace' => 'Etsy', 'total_stickers' => 8, 'total_orders' => 4, 'total_value' => 34.920000000000002],
['id' => 5, 'date_inserted' => '2020-09-07', 'marketplace' => 'Woo', 'total_stickers' => 9, 'total_orders' => 3, 'total_value' => '52.90'],
['id' => 6, 'date_inserted' => '2020-09-07', 'marketplace' => 'eBay', '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
)
)
)
推荐阅读
- flutter - 在显示图像的gridview上添加手势检测器的位置
- php - 为 PHPWord 中的所有链接标签添加样式
- javascript - 如何使用中间件获取nodejs中rest api的执行时间(获得响应的时间)和api
- azure - 一个 Cosmos DB 查询中的 TOP 和 DISTINCT
- python - 如何将此数据框添加到 HDF5 文件中的数据集中?
- java - 如何在我的全局变量中存储一个值并在活动中的任何方法中使用它
- sql - Sql - 打开 Json 插入表
- command-line - Loopback cli (lb4) 在启动时崩溃
- typescript - 由于“TypeError:无法读取未定义的属性 'xxx'”,Jest 测试套件无法运行
- python - 编写代码以检查密码是否符合特定条件