php 数组,将日期作为键然后合并相同的日期?
php array, make date the keys then merge same dates?
我的数组(存储在 $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
)
)
)
我的数组(存储在 $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
)
)
)