开发者

merge arrays within two arrays that share a common index - php

I have two arrays that have common indexes (church and office). I need to "merge" the total of the first array into the second array to get the desired output (seen below the double line). I'm not sure how to do this with array_merge(). Any help would be greatly appreciated!

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

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

)

Array
(
    [church] => Array
        (

            [name] => Array
                开发者_StackOverflow(
                    [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
                )

        )

)


Try something like this:

$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]);
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜