使用一列值作为第一层级,另一列值作为第二层级,将2D数组转换为3D关联数组。

3

我的数组(存储在 $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]不是必需的,"marketplace"可以成为内部数组的键,因此我想要的最终输出应该像这样。

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
                )

        )
)

现在我需要让它显示当天的正确数据。


1
你能展示一下你尝试过的东西吗,即使它没有成功吗? - Lawrence Cherone
1
这个问题有回答了吗?如何通过列值对子数组进行分组? - floGalen
@LawrenceCherone 我已经更新了原帖,你的答案几乎解决了我的问题,但现在我遇到了一个问题,它显示所有天数的值都相同(实际上它们并不相同)。 - Glen Keybit
2个回答

1
尝试使用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);

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
                )

        )

)

https://3v4l.org/QnHQf


0

将您的二维数组重构为三维数组只需使用一个循环即可。只需从每行中提取级别标识符并将它们用作新级别键。

代码:(演示)

$result = [];
foreach ($array as $row) {
    $level1 = $row['date_inserted'];
    $level2 = $row['marketplace'];
    unset($row['date_inserted'], $row['marketplace']);
    $result[$level1][$level2] = $row;
}
var_export($result);

或者使用array_splice()函数:(演示)

$result = [];
foreach ($array as $row) {
    ['date_inserted' => $level1, 'marketplace' => $level2] = array_splice($row, 1, 2);
    $result[$level1][$level2] = $row;
}
var_export($result);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接