合并两个共享一个公共索引的数组中的数组 - php

时间:2011-07-26 18:49:24

标签: php arrays array-merge

我有两个具有共同索引(教堂和办公室)的数组。我需要将第一个数组的总和“合并”到第二个数组中以获得所需的输出(见双线下方)。我不知道如何使用array_merge()来做到这一点。任何帮助将不胜感激!

Array
(
    [church] => Array
        (
            [total] => 77
        )

    [office] => Array
        (
            [total] => 202
        )

)

Array
(
    [church] => Array
        (

            [name] => Array
                (
                    [0] => Bill
                    [1] => Sally
                )

            [addr] => Array
                (
                    [0] => Address Same as Billing
                    [1] => Address Same as Billing
                )

            [message] => Array
                (
                    [0] => 
                    [1] => 
                )

            [amount] => Array
                (
                    [0] => 25
                    [1] => 50
                )

        )

    [office] => Array
        (

            [name] => Array
                (
                    [0] => Marta
                    [1] => Ruth
                )

            [addr] => Array
                (
                    [0] => Address Same as Billing
                    [1] => Address Same as Billing
                )

            [message] => Array
                (
                    [0] => 
                    [1] => 
                )

            [amount] => Array
                (
                    [0] => 100
                    [1] => 100
                )

        )

)

=============================================== =====

Array
(
    [church] => Array
        (
        [total] => 77

            [name] => Array
                (
                    [0] => Bill
                    [1] => Sally
                )

            [addr] => Array
                (
                    [0] => Address Same as Billing
                    [1] => Address Same as Billing
                )

            [message] => Array
                (
                    [0] => 
                    [1] => 
                )

            [amount] => Array
                (
                    [0] => 25
                    [1] => 50
                )

        )

    [office] => Array
        (
        [total] => 202

            [name] => Array
                (
                    [0] => Marta
                    [1] => Ruth
                )

            [addr] => Array
                (
                    [0] => Address Same as Billing
                    [1] => Address Same as Billing
                )

            [message] => Array
                (
                    [0] => 
                    [1] => 
                )

            [amount] => Array
                (
                    [0] => 100
                    [1] => 100
                )

        )

)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$ a = array('church'=> array('total'=> 5),'office'=> array('total'=> 10));
$ b = array('church'=> array('name'=>'church'),'office'=> array('name'=>'office'));

foreach($ b as $ key => $ value){
$ b [$ key] = array_merge($ a [$ key],$ b [$ key]);
}