1

我有这个树形式的多维数组。

$menu = [
    'records' => [
        [
            'id' => 1,
            'parent_id' => null,
            'name' => 'Food',
            'type' => 'category',
            'records' => [
                [
                    'id' => 2,
                    'parent_id' => 1,
                    'name' => 'Breakfast',
                    'type' => 'category',
                    'records' => [
                        [
                            'id' => 3,
                            'parent_id' => 2,
                            'name' => 'Omelette',
                            'type' => 'category',
                            'records' => [
                                [
                                    'id' => 4,
                                    'parent_id' => 3,
                                    'name' => 'Turkish',
                                    'type' => 'item'
                                ],
                                [
                                    'id' => 5,
                                    'parent_id' => 3,
                                    'name' => 'Indian',
                                    'type' => 'item'
                                ],
                                [
                                    'id' => 6,
                                    'parent_id' => 1,
                                    'name' => 'American',
                                    'type' => 'item'
                                ]
                            ],
                            'statistics' => [
                                'sale_qty' => 0,
                                'gross_sale' => 0,
                                'net_sale' => 0,
                                'comp_qty' => 0,
                                'comp_total' => 0
                            ]
                        ],
                        [
                            'id' => 7,
                            'parent_id' => 1,
                            'name' => 'Bread',
                            'type' => 'item'
                        ]
                    ],
                    'statistics' => [
                        'sale_qty' => 0,
                        'gross_sale' => 0,
                        'net_sale' => 0,
                        'comp_qty' => 0,
                        'comp_total' => 0
                    ]
                ],
                [
                    'id' => 8,
                    'parent_id' => 1,
                    'name' => 'Jam',
                    'type' => 'item'
                ]
            ],
            'statistics' => [
                'sale_qty' => 0,
                'gross_sale' => 0,
                'net_sale' => 0,
                'comp_total' => 0
            ]
        ]
    ],
    'statistics' => [
        'sale_qty' => 0,
        'gross_sale' => 0,
        'net_sale' => 0,
        'comp_qty' => 0,
        'comp_total' => 0
    ]
];

然后我有另一个日志数组

 $log = [
    'ids' => [5, 8],
    5 => [
        'id' => 5,
        'parent_id' => 3,
        'name' => 'Indian',
        'type' => 'item',
        'item_price' => '475.00',
        'item_tax' => '76.000000',
        'sale_count' => '1',
        'gross_total' => 551,
        'complimentry_count' => '0',
        'complimentry_total' => 0,
        'complimentry_net_total' => 0,
        'net_total' => 475
    ],
    8 => [
        'id' => 8,
        'parent_id' => 1,
        'name' => 'Jam',
        'type' => 'item',
        'item_price' => '603.45',
        'item_tax' => '"96.55',
        'sale_count' => '1',
        'gross_total' => 700,
        'complimentry_count' => '0',
        'complimentry_total' => 0,
        'complimentry_net_total' => 0,
        'net_total' => 603.45
    ]
];

我在这里要做的是通过匹配'id'键来搜索数组$log['ids']中的值。$menu如果在 中找到该值,$menu则将其替换为在 中找到的适当数组$log

例如,$menu如果找到值 5 将其替换为$log[5].

完成此操作后,其余不需要且不在路径中的节点将被删除,并且每个叶子(项目)的 sale_count 等的总和将等于该叶子直接父统计节点的值。

最终数组会是这样的

$finalmenu = [
    'records' => [
        [
            'id' => 1,
            'parent_id' => null,
            'name' => 'Food',
            'type' => 'category',
            'records' => [
                [
                    'id' => 2,
                    'parent_id' => 1,
                    'name' => 'Breakfast',
                    'type' => 'category',
                    'records' => [
                        [
                            'id' => 3,
                            'parent_id' => 2,
                            'name' => 'Omelette',
                            'type' => 'category',
                            'records' => [
                                [
                                    'id' => 5,
                                    'parent_id' => 3,
                                    'name' => 'Indian',
                                    'type' => 'item',
                                    'item_price' => '475.00',
                                    'item_tax' => '76.000000',
                                    'sale_count' => '1',
                                    'gross_total' => 551,
                                    'complimentry_count' => '0',
                                    'complimentry_total' => 0,
                                    'complimentry_net_total' => 0,
                                    'net_total' => 475
                                ]
                            ],
                            'statistics' => [
                                'sale_qty' => 1,
                                'gross_sale' => 551,
                                'net_sale' => 475,
                                'comp_qty' => 0,
                                'comp_total' => 0
                            ]
                        ],
                    ],
                    'statistics' => [
                        'sale_qty' => 1,
                        'gross_sale' => 551,
                        'net_sale' => 475,
                        'comp_qty' => 0,
                        'comp_total' => 0
                    ]
                ],
                [
                    'id' => 8,
                    'parent_id' => 1,
                    'name' => 'Jam',
                    'type' => 'item',
                    'item_price' => '603.45',
                    'item_tax' => '96.55',
                    'sale_count' => '1',
                    'gross_total' => 700,
                    'complimentry_count' => '0',
                    'complimentry_total' => 0,
                    'complimentry_net_total' => 0,
                    'net_total' => 603.45
                ]
            ],
            'statistics' => [
                'sale_qty' => 2,
                'gross_sale' => 1251,
                'net_sale' => 1078.45,
                'comp_total' => 0
            ]
        ]
    ],
    'statistics' => [
        'sale_qty' => 2,
        'gross_sale' => 1251,
        'net_sale' => 1078.45,
        'comp_total' => 0
    ]
];

我一直在努力完成这个工作大约一个星期,但我只能拿到替换零件,但在那之后就不行了。我试过RecursiveIteratorIterator了,RecursiveArrayIterator但没有用。

如果有人对此有答案,将不胜感激。

谢谢。

4

0 回答 0