-1

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
            )

需要帮忙

4

1 回答 1

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
        )

)
于 2013-02-27T06:57:34.747 回答