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

时间:2014-03-19 16:55:38

标签: php arrays array-merge

我在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
    )

)

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
    )

)

我需要使用expense_id 组合2个数组,结果必须如下所示:

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
                    )
            )           

    )

)

正如您所看到的,我在两个阵列上都有 expense_id 需要成为最终Array3的关键

我试图循环一个数组并检查expense_id键匹配但我无法实现我的最终数组。 另外,我看了 array_merge array_map array_intersect

感谢您的建议。

2 个答案:

答案 0 :(得分:1)

尝试类似......

foreach($object AS $key=>$element){ if ($element->expense_id == 475){ $element->expense_taxes[0] = $stdClass; } }

答案 1 :(得分:0)

这有点复杂,因为实际的对象数组是从ajax到PHP的,但是,我发现自己是基于KennyDope建议的答案。

如果Array1是$ expenses_obj而Array2是$ 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;

最终结果是我需要的:

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
                    )

            )

    )

)

欢迎您的评论。感谢您的投入。