Problems adding one number to another in several matrices

0

The problem is that I add the positions of the matrices instead of the value

$totalVotos = Array
(



 [0] => Array
        (
            [votos] => 8
            [0] => 8
        )

[1] => Array
    (
        [votos] => 9
        [0] => 9
    )

[2] => Array
    (
        [votos] => 4
        [0] => 4
    )

[3] => Array
    (
        [votos] => 10
        [0] => 10
    )

)



foreach ($totalVotos as $numeros) {

            $sumar = $numeros['0'];
            $sumar = explode(' ', $sumar);
            print_r(array_sum($sumar));
        }
    
asked by JOSE HERRADA 15.08.2018 в 22:24
source

2 answers

0

try with a compiler like this:

<?php
$totalVotos = Array
(
 0 => Array
        (
            'votos' => 8,
            0 => 8
        ),

1 => Array
    (
        'votos' => 9,
        0 => 9
    ),

2 => Array
    (
        'votos' => 4,
        0 => 4
    ),

3 => Array
    (
        'votos' => 10,
        0 => 10
    ),

);
$sumar=0;
foreach ($totalVotos as $numeros) {
    $sumar += $numeros['0'];
}
print_r($sumar);

but if I did it would be something like this:

print_r(array_sum(array_column($totalVotos, 'votos')));
    
answered by 15.08.2018 в 22:38
0

I leave you with some code so you can see how to run lodged arrays with foreach , it would be something like this:

foreach ($totalVotos as $segundoArray) {
    //declaras una variable que almacenara la sumatoria del cada bucle interno
    $sumar=0;
    foreach ($segundoArray as $valorVoto) {
    //sumas los valores en cada vuelta
        $sumar = $sumar + $valorVoto;
    }

    //imprimes los valores de cada ciclo interno
    print_r(array_sum($sumar));
}

I hope you serve, you tell me how it went

    
answered by 15.08.2018 в 22:39