0

After thinking and thinking I don´t find a way on how to solve this problem. I have been thinking of a loop inside a loop but I always reach to no end.

Having multidimensional arrays like these:

0 => {'id' => (int) 1,
      'meritos' => [
        'id' => '39',
        'Puntuacion' => '6'
        ],
      'categorias' => [
        'id' => '29',
        'PuntuacionMax' => '15'
        ]   
    },
1 => {'id' => (int) 3,
      'meritos' => [
        'id' => '40',
        'Puntuacion' => '7'
        ],
      'categorias' => [
        'id' => '30',
        'PuntuacionMax' => '10'
        ]   
    },
2 => {'id' => (int) 4,
      'meritos' => [
        'id' => '41',
        'Puntuacion' => '8'
        ],
      'categorias' => [
        'id' => '30',
        'PuntuacionMax' => '10'
        ]   
    },
3 => {'id' => (int) 4,
      'meritos' => [
        'id' => '42',
        'Puntuacion' => '8'
        ],
      'categorias' => [
        'id' => '29',
        'PuntuacionMax' => '15'
        ]   
    },
etc...

I need to sum all 'meritos' => 'Puntuacion' but with the following inconvenience:

The sum of 'meritos' => 'Puntuacion' with the same 'categorias' => 'id' can´t be superior to 'categorias' => 'PuntuacionMax'

I.E. with the arrays I gave.

I have two 'puntuacion' values that belongs to the same 'categorias' => 'id' (29) which are 8 and 6. The sum would be 14 and doesn´t surpass the puntuacionMax (15) so it´s ok.

The other two 'puntuacion' values that belongs to the same 'categorias' => 'id' (30) are 7 and 8. The sum would be 15 but surpass the puntuacionMax (10) so the result must be 10.

The final sum value would be 24 (sum from 14 and 10)

Thanks in advance for any help.

Gautam Rai
  • 2,445
  • 2
  • 21
  • 31

1 Answers1

0

I think result should be something like this.

$resultArray = array();

foreach ($data as $item) {
    if (!isset($resultArray[$item['categorias']['id']])) $resultArray[$item['categorias']['id']] = array('Puntuacion' => 0, 'PuntuacionMax' => $item['categorias'][PuntuacionMax]);
    $resultArray[$item['categorias']['id']]['Puntuacion'] += $item['meritos']['Puntuacion'];
}

$result = 0;
foreach ($resultArray as $row) {
    if ($row['Puntuacion'] > $row['PuntuacionMax']) $row['Puntuacion'] = $row['PuntuacionMax'];
    $result += $row['Puntuacion'];
}
  • Thanks a lot for your reply Jaakko! but I believe you have a mistake on the line: `$resultArray[$item['categorias']['id']] += $item['meritos']['Puntuacion'];` Did you meant `$resultArray['Puntuacion'] += $item['meritos']['Puntuacion'];`? Cause I cannot see too much sense on that line and also I get an `Unsupported operand types` error due sum an array with a value. Anyway I tried changing as I said and the final result is giving 0 so I believe your algorithm is failling somewhere? – Pablo Portillo Jun 18 '18 at 08:21
  • I solved it by edditing that line to: `$resultArray[$item['categorias']['id']]['Puntuacion'] += $item['meritos']['Puntuacion'];` Thanks a lot Jakko, really appreciated! – Pablo Portillo Jun 18 '18 at 08:45