1

共通のインデックス (教会とオフィス) を持つ 2 つの配列があります。目的の出力を得るには、最初の配列の合計を 2 番目の配列に「マージ」する必要があります (二重線の下に表示されます)。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
                )

        )

)
4

1 に答える 1

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

于 2011-07-26T19:07:39.553 に答える