duanjing7459 2014-03-19 16:55
浏览 217
已采纳

如果另一个对象元素id数组具有相同的id,如何将新元素添加(更新)到对象数组中?

I have these arrays in PHP:

Array1
(
[0] => stdClass Object
    (
        [expense_id] => 475
        [expense_name] => DRAY 
        [expense_unit_cost] => 270.00
    )

[1] => stdClass Object
    (
        [expense_id] => 476
        [expense_name] => FUEL 
        [expense_unit_cost] => 32.40
    )

)

and

Array2
(
[0] => stdClass Object
    (
        [waybill_id] => 20005044
        [expense_id] => 475
        [tax_select] => tax1
        [tax_id] => 1
        [tax_name] => GST 5%
        [tax_no] => 
        [tax_value] => 13.5000
    )

[1] => stdClass Object
    (
        [waybill_id] => 20005044
        [expense_id] => 475
        [tax_select] => tax2
        [tax_id] => 2
        [tax_name] => QST 9.975%
        [tax_no] => 
        [tax_value] => 26.9325
    )

[2] => stdClass Object
    (
        [waybill_id] => 20005044
        [expense_id] => 476
        [tax_select] => tax1
        [tax_id] => 1
        [tax_name] => GST 5%
        [tax_no] => 
        [tax_value] => 1.6200
    )

[3] => stdClass Object
    (
        [waybill_id] => 20005044
        [expense_id] => 476
        [tax_select] => tax2
        [tax_id] => 2
        [tax_name] => QST 9.975%
        [tax_no] => 
        [tax_value] => 3.2319
    )

)

I need to combine the 2 arrays using expense_id and the result have to look like this:

Array3
(
[0] => stdClass Object
    (
        [expense_id] => 475
        [expense_name] => DRAY 
        [expense_unit_cost] => 270.00
        [expense_taxes] => Array
            (
                [0] => stdClass Object
                    (
                        [waybill_id] => 20005044
                        [expense_id] => 475
                        [tax_select] => tax1
                        [tax_id] => 1
                        [tax_name] => GST 5%
                        [tax_no] => 
                        [tax_value] => 13.5000
                    )

                [1] => stdClass Object
                    (
                        [waybill_id] => 20005044
                        [expense_id] => 475
                        [tax_select] => tax2
                        [tax_id] => 2
                        [tax_name] => QST 9.975%
                        [tax_no] => 
                        [tax_value] => 26.9325
                    )
            )       

    )

[1] => stdClass Object
    (
        [expense_id] => 476
        [expense_name] => FUEL 
        [expense_unit_cost] => 32.40
        [expense_taxes] => Array
            (
                [0] => stdClass Object
                    (
                        [waybill_id] => 20005044
                        [expense_id] => 476
                        [tax_select] => tax1
                        [tax_id] => 1
                        [tax_name] => GST 5%
                        [tax_no] => 
                        [tax_value] => 1.6200
                    )

                [1] => stdClass Object
                    (
                        [waybill_id] => 20005044
                        [expense_id] => 476
                        [tax_select] => tax2
                        [tax_id] => 2
                        [tax_name] => QST 9.975%
                        [tax_no] => 
                        [tax_value] => 3.2319
                    )
            )           

    )

)

As you can see I have on both arrays the expense_id which need to be the key for the final Array3

I have tried to loop one array and check for the expense_id key matching but I can't achieve my final array. Also, I have took a look at array_merge, array_map and array_intersect.

Thank you for your suggestions.

  • 写回答

2条回答 默认 最新

  • duanchen7703 2014-03-19 20:12
    关注

    It's all a bit more complicate because the actual arrays of objects are got from ajax into PHP, but, I found myself the answer based on KennyDope suggestion.

    If Array1 is $expenses_obj and Array2 is $invoice_taxes_obj

            foreach ($expenses_obj as $key => $expense):
                foreach ($invoice_taxes_obj as $key => $tax):
                    if ($expense->expense_id == $invoice_taxes_obj[$key]->expense_id){
                        $expense->taxes[] = (array)$invoice_taxes_obj[$key]; 
                    }   
                endforeach;
            endforeach;
    

    And the final result is what I need:

    Array
    (
    [0] => stdClass Object
        (
            [expense_id] => 475
            [expense_name] => DRAY 
            [expense_unit_cost] => 270.00
            [taxes] => Array
                (
                    [0] => Array
                        (
                            [waybill_id] => 20005044
                            [expense_id] => 475
                            [tax_select] => tax1
                            [tax_id] => 1
                            [tax_name] => GST 5%
                            [tax_no] => 
                            [tax_value] => 13.5000
                        )
    
                    [1] => Array
                        (
                            [waybill_id] => 20005044
                            [expense_id] => 475
                            [tax_select] => tax2
                            [tax_id] => 2
                            [tax_name] => QST 9.975%
                            [tax_no] => 
                            [tax_value] => 26.9325
                        )
    
                )
    
        )
    
    [1] => stdClass Object
        (
            [expense_id] => 476
            [expense_name] => FUEL 
            [expense_unit_cost] => 32.40
            [taxes] => Array
                (
                    [0] => Array
                        (
                            [waybill_id] => 20005044
                            [expense_id] => 476
                            [tax_select] => tax1
                            [tax_id] => 1
                            [tax_name] => GST 5%
                            [tax_no] => 
                            [tax_value] => 1.6200
                        )
    
                    [1] => Array
                        (
                            [waybill_id] => 20005044
                            [expense_id] => 476
                            [tax_select] => tax2
                            [tax_id] => 2
                            [tax_name] => QST 9.975%
                            [tax_no] => 
                            [tax_value] => 3.2319
                        )
    
                )
    
        )
    
    )
    

    Your comments are welcomed. Thanks for your input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿