1

I have two arraws where I'm storing some data: activos[key][value] and pagos[key][value]

the operation I want to do is for each "activo" element get the value and where it coincides with the key on pagos sum the value of this array to a variable.

I've tryed with this but it doesn't work as it is supposed to:

$t = 0;
foreach ($activos as $key => $value) {
    foreach ($pagos as $key => $value) {
        if($activos[$value] == $pagos[$key]){
            $t += $pagos[$key]
        }
    }
}
4
  • ; missing after $t += $pagos[$key]; Commented Mar 17, 2016 at 10:12
  • well true but there is not the problem, it doesn't do what its supposed Commented Mar 17, 2016 at 10:14
  • $key & $value overwriting here :) see in answer; Commented Mar 17, 2016 at 10:16
  • It's rather ambiguous what you want to express with that activos[key][value] notation; a print_r sample of your actual array would say a lot more. Commented Mar 17, 2016 at 10:16

3 Answers 3

2

You've already key and value from both arrays, but you've erased them !!

$t = 0;
foreach ($activos as $keyActivos => $valueActivos) {
    foreach ($pagos as $keyPagos => $valuePagos) {
        if($valueActivos == $keyPagos){
            $t += $valuePagos;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This made the solution, tnks!
2

$key & $value overwriting here :)

$t = 0;
foreach ($activos as $key => $value) {
  foreach ($pagos as $key1 => $value1) {
      if($activos[$value] == $pagos[$key1]){
          $t += $pagos[$key1];
      }
  }
}

Comments

0

I thinks this is what you need

$t = 0;
foreach ($activos as $key => $value) {
    foreach ($pagos as $key1 => $value1) {
        if($value == $key1){
            $t += $value1;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.