-1

phparray_sum多次元配列各配列の文字の合計を取得したいと思います。私のコードを見てください

配列(/ ----------------- help --------- /

[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 に答える