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

发布于 2024-11-26 14:21:08 字数 2985 浏览 1 评论 0原文

我有两个具有共同索引的数组(教堂和办公室)。我需要将第一个数组的总和“合并”到第二个数组中以获得所需的输出(见双线下方)。我不知道如何使用 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
                )

        )

)

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

        )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-12-03 14:21:08

尝试这样的操作:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文