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

时间:2021-05-10 13:44:22

I have these arrays in PHP:

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

我需要使用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
                    )
            )           

    )

)

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

正如你所看到的,我在两个数组上都有expense_id,它需要是最终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.

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

Thank you for your suggestions.

谢谢你的建议。

2 个解决方案

#1


1  

Try something like...

试试像......

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

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

#2


0  

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.

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

If Array1 is $expenses_obj and Array2 is $invoice_taxes_obj

如果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;

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


1  

Try something like...

试试像......

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

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

#2


0  

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.

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

If Array1 is $expenses_obj and Array2 is $invoice_taxes_obj

如果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;

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.

欢迎您的意见。感谢您的输入。