array_sum多维数组

时间:2013-02-27 06:25:52

标签: multidimensional-array

PHP array_sum多维数组 我想得到每个数组的字母总和。请看一下我的代码

阵 ( / ----------------- ---------帮助 /

[0] => Array
    (
        [0] => Array
            (
                [a] => 1
                [b] => 0
                [c] => 0
                [d] => 0
            )

        [1] => Array
            (
                [a] => 0
                [b] => 1
                [c] => 0
                [d] => 0
            )

    )
[1] => Array
    (
        [0] => Array
            (
                [a] => 1
                [b] => 0
                [c] => 0
                [d] => 0
            )

        [1] => Array
            (
                [a] => 0
                [b] => 1
                [c] => 0
                [d] => 0
            )

    )

) 每个数组的总和 想要这样的结果 请帮忙

     [0] => Array
            (
                [a] => 2
                [b] => 0
                [c] => 0
                [d] => 0
            )
        [1] => Array
            (
                [a] => 0
                [b] => 2
                [c] => 0
                [d] => 0
            )

需要帮助

1 个答案:

答案 0 :(得分:0)

没有任何标签,我用PHP解释,因为这是我从直觉中看到的东西:)

一旦我知道您的语言支持,我一定会编辑我的帖子。

<?php

$myarr = Array(
        0 => Array
            (
                'a' => 1,
                'b' => 0,
                'c' => 0,
                'd' => 0,
            ),
            1=> Array
            (
                'a' => 0,
                'b' => 1,
                'c' => 0,
                'd' => 0,
            ),

        2 => Array
            (
                'a' => 0,
                'b' => 0,
                'c' => 1,
                'd' => 0,
            ),

    );

foreach($myarr as &$qwe )
{
 foreach($qwe as $key => &$value)
{
if($value == 1 )
$value++;
}
}

print_r($myarr);

<强>输出

Array
(
    [0] => Array
        (
            [a] => 2
            [b] => 0
            [c] => 0
            [d] => 0
        )

    [1] => Array
        (
            [a] => 0
            [b] => 2
            [c] => 0
            [d] => 0
        )

    [2] => Array
        (
            [a] => 0
            [b] => 0
            [c] => 2
            [d] => 0
        )

)