Maison >développement back-end >tutoriel php >二维数组根据键值相加

二维数组根据键值相加

WBOY
WBOYoriginal
2016-06-23 14:15:221400parcourir

怎样根据每一个数组的键值相加?


Array
(
    [0] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 7
            [pay] => 0
            [buy] => 4
            [payment] => 11334.00
            [recharge] => 1
            [rechargea] => 3600.00
        )

    [1] => Array
        (
            [reg] => 1
            [regs] => 0
            [login] => 22
            [pay] => 0
            [buy] => 0
            [payment] => 0.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [2] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 5
            [pay] => 0
            [buy] => 1
            [payment] => 376.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [3] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 3
            [pay] => 0
            [buy] => 0
            [payment] => 0.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [4] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 2
            [pay] => 0
            [buy] => 1
            [payment] => 1787.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [5] => Array
        (
            [reg] => 1
            [regs] => 0
            [login] => 7
            [pay] => 0
            [buy] => 0
            [payment] => 0.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [6] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 1
            [pay] => 0
            [buy] => 0
            [payment] => 0.00
            [recharge] => 0
            [rechargea] => 0.00
        )

    [7] => Array
        (
            [reg] => 0
            [regs] => 0
            [login] => 0
            [pay] => 0
            [buy] => 0
            [payment] => 0.00
            [recharge] => 0
            [rechargea] => 0.00
        )

)


回复讨论(解决方案)

期望结果是什么?? 描述一下怎么得来的。

期望结果是什么?? 描述一下怎么得来的。

期望结果是什么?? 描述一下怎么得来的。

array[0][reg] + array[1][reg]+ array[2][reg]......

array[0][login] + array[1][login]+ array[2][login]......

每一个数组相同键值的相加在一起。

$data = array(0 => array('a'=>1, 'b' => 2), 1 => array('a' => 3, 'c' => 4));$result = array();foreach($data as $index => $arr){    foreach($arr as $key => $value){        $result[$key] += $value;    }}print_r($result);

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn