How to join values in common php array?

Asked

Viewed 687 times

2

I have the array

$vetor[0]['codigo'] = '1';
$vetor[0]['valor']  = '4';

$vetor[1]['codigo'] = '1';
$vetor[1]['valor']  = '2';

$vetor[2]['codigo'] = '2';
$vetor[2]['valor']  = '2';

I need to merge all values with equal codes. Making the above example look like this:

Code 1 is set to 6

And code 2 is set to 2

Does anyone have any idea how to do that?

  • vc speaks sum the values $vector[n]['code] to get only one with the total?

  • 1

    To merge all values with equal code

3 answers

1

I didn’t have time to test it, but if you follow the logic, it should work.

    $resultado = [];
        for(i =0; i<sizeof($vetor); i++){
            $resultado[$vetor[i]] = $resultado[$vetor[i]] + $vetor[i]['valor'];
        }

In this code the position of the result vector will be id

  • Parse error: syntax error, Unexpected '=', expecting ';' in

  • I’m not finding the error in the code

  • I guess it’s just the dot and comma after the line

  • $result[$vector[i]] = $result[$vector[i]] + $vector[i]['value'];

1


I would do so:

$arrayFinal = array();

for($x = 0; $x < count($vetor); $x++){

    $codigo = $vetor[$x]['codigo'];
    if(array_key_exists($codigo, $arrayFinal)){
        $arrayFinal[$codigo]['valor'] += $vetor[$x]['valor'];
        continue;
    }
    $arrayFinal[$codigo] = array("codigo" => $codigo, "valor" => $vetor[$x]['valor']);
}
print_r($arrayFinal);

This way it will add only the elements that have the same code.

  • I was using the code but gave a problem, the $vector it is a matrix. And this it is adding up the values of different arrays. For example. https://notepad.pw/arrayemployment. In the third array The value of 5405 vProd is returning 1103.53 But the correct value is 355.00. There is something to give += that is mixing the arrays

  • @Pauloroberto the code is CFOP is this? And the value is right vProd?

  • That’s right CFOP and vProd

  • I managed to solve, I put an unset at the end of the for and already worked Vlw

  • Glad you made it to @Pauloroberto ! Hug!

0

You can do it like this:

$codigo=$valor=0;
for ($i=0; $i < count($vetor); $i++) { 
    $codigo+=$vetor[$i]['codigo'];
    $valor+=$vetor[$i]['valor'];
}
unset($vetor);//para eliminar o array vetor anterior
$vetor['codigo']=$codigo;
$vetor['valor']=$valor;
print_r($vetor);
  • So it sums the whole array into one array. I need it to merge the values of everything that has the same code. Not to create duplicate codes.

  • So that the result of the example is two codes code 1 and 2 but the value of code 1 will be 6 while of code 2 will be 2

  • I get it, I’ll fix it

  • always follows this pattern ne?

  • Always will have a code and a value, same pattern can appear new codes but will continue the same structure

  • you who assembles this structure?

  • The structure yes, however the content of the code and everything else comes from an xml

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.