-2
Array
(
    [Apr] => Array
        (
            [0] => 67
            [1] => 392
            [2] => 140
            [3] => 250
        )

    [May] => Array
        (
            [0] => 67
            [1] => 392
            [2] => 140
            [3] => 250
        )
)   

我想要一个结果数组,它将像这样保存总和

Array
(
    [Apr] => Array
        (
            [0] => 849
        )

    [May] => Array
        (
            [0] => 849
        )
)
4

2 回答 2

1

@Rakesh Tripathi 只需尝试使用 foreach() 和 array_sum() ,如下所示:

<?php
$array =     array(
                "Apr" => array(
                        67,
                        392,
                        140,
                        250
                    ),
                "May" => array(
                        67,
                        392,
                        140,
                        250
                    )
            );
foreach ($array as $key => $value) {
    $array[$key] = array_sum($value);
}
echo "<pre>";
print_r($array);
于 2017-04-14T07:49:04.747 回答
0

array_map()这是一个使用and的简洁的函数式语法array_sum()

代码:(演示

$array=[
    "Apr"=>[67,392,140,250],
    "May"=>[67,392,140,250]
];

var_export(array_map('array_sum',$array));

输出:

array (
  'Apr' => 849,
  'May' => 849,
)
于 2017-12-17T09:00:50.303 回答