php -如何求和多维数组。

时间:2021-05-16 21:27:54

I have data below :

我有以下数据:

Array(
        [A] => Array
            (
                [AA] => 10
            )

        [B] => Array
            (
                [BA] => 5
                [BB] => 1
                [BC] => -2
            )

        [C] => Array
            (
                [CA] => 3
                [CB] => 0
            )
    )

I want to sum the value of second element my array (BA,BB,BC, etc) like this :

我想要将数组(BA,BB,BC等)的第二个元素的值求和:

    Array(
        [A] => 10        
        [B] => 4
        [C] => 3
    )

I've tried to do with foreach (I'm using php as my platform) but the result is wrong, can someone give me explanation and the logic to solve this? thanks

我试过用foreach(我用php作为我的平台),但是结果是错误的,有人能给我解释一下吗?谢谢

3 个解决方案

#1


3  

You can loop thru your array and use array_sum

可以通过数组进行循环并使用array_sum

$arr = array(
    "A" => array
        (
            "AA" => 10,
        ),

    "B" => array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),

    "C" => array
        (
            "CA" => 3,
            "CB" => 0
        )
);

$result = array();
foreach( $arr as $key => $val ){
    $result[$key] = array_sum ( $val );
}

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

这将导致:

Array
(
    [A] => 10
    [B] => 4
    [C] => 3
)

Doc: http://php.net/manual/en/function.array-sum.php

医生:http://php.net/manual/en/function.array-sum.php

#2


1  

This should work for arrays like the one of your example:

这应该适用于数组,比如您的示例:

 $arr = array(
    "A" => array
        (
            "AA" => 10,
        ),

    "B" => array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),

    "C" => array
        (
            "CA" => 3,
            "CB" => 0
        )
);
$res = array();
foreach($arr as $key => $value) {
        foreach($value as  $number) {
            (!isset($res[$key])) ?
                $res[$key] = $number :
                $res[$key] += $number;
        }
    }



echo "<pre>";
print_r( $res );
echo "</pre>";

This is working without using an inbuilt function.

这是在不使用内置函数的情况下工作的。

#3


0  

if you want sum column

如果你想要和列

<?php
$array = array
    (
      "A"=>array
        (
"AA" => 10,
        ),
      "B"=>array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),
      "C"=>array
        (
            "CA" => 3,
            "CB" => 0
         )
        );

foreach ($array as $key=>$value)
{
$mehrdad[]=$key;
}
foreach ($mehrdad as $key1=>$value1)
{
    $arrays=$array[$value1];
foreach ($arrays as $key2=>$value2)
{

$mehrdadi[]=$key2;
}

}
$mehrdadend=array_unique($mehrdadi);
$mehrdadis = array();
foreach ($mehrdadend as $key3=>$value3)
{
$sum=array_sum(array_column($array, $value3));

$mehrdadis[$value3] = $sum;
}
print_r($mehrdadis);
?>

Result

结果

Array
(
    [AA] => 10
    [BA] => 5
    [BB] => 1
    [BC] => -2
    [CA] => 3
    [CB] => 0
)

#1


3  

You can loop thru your array and use array_sum

可以通过数组进行循环并使用array_sum

$arr = array(
    "A" => array
        (
            "AA" => 10,
        ),

    "B" => array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),

    "C" => array
        (
            "CA" => 3,
            "CB" => 0
        )
);

$result = array();
foreach( $arr as $key => $val ){
    $result[$key] = array_sum ( $val );
}

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

这将导致:

Array
(
    [A] => 10
    [B] => 4
    [C] => 3
)

Doc: http://php.net/manual/en/function.array-sum.php

医生:http://php.net/manual/en/function.array-sum.php

#2


1  

This should work for arrays like the one of your example:

这应该适用于数组,比如您的示例:

 $arr = array(
    "A" => array
        (
            "AA" => 10,
        ),

    "B" => array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),

    "C" => array
        (
            "CA" => 3,
            "CB" => 0
        )
);
$res = array();
foreach($arr as $key => $value) {
        foreach($value as  $number) {
            (!isset($res[$key])) ?
                $res[$key] = $number :
                $res[$key] += $number;
        }
    }



echo "<pre>";
print_r( $res );
echo "</pre>";

This is working without using an inbuilt function.

这是在不使用内置函数的情况下工作的。

#3


0  

if you want sum column

如果你想要和列

<?php
$array = array
    (
      "A"=>array
        (
"AA" => 10,
        ),
      "B"=>array
        (
            "BA" => 5,
            "BB" => 1,
            "BC" => -2
        ),
      "C"=>array
        (
            "CA" => 3,
            "CB" => 0
         )
        );

foreach ($array as $key=>$value)
{
$mehrdad[]=$key;
}
foreach ($mehrdad as $key1=>$value1)
{
    $arrays=$array[$value1];
foreach ($arrays as $key2=>$value2)
{

$mehrdadi[]=$key2;
}

}
$mehrdadend=array_unique($mehrdadi);
$mehrdadis = array();
foreach ($mehrdadend as $key3=>$value3)
{
$sum=array_sum(array_column($array, $value3));

$mehrdadis[$value3] = $sum;
}
print_r($mehrdadis);
?>

Result

结果

Array
(
    [AA] => 10
    [BA] => 5
    [BB] => 1
    [BC] => -2
    [CA] => 3
    [CB] => 0
)