I have an array as follows:
$array = [
[
'result' => null,
'weight' => 50,
'children' => [
['result' => 5, 'weight' => 50, 'children' => []],
['result' => 5, 'weight' => 50, 'children' => []]
]
],
[
'result' => 3,
'weight' => 50,
'children' => []
],
];
What I would like to achieve is run a function, like:
function calculateScores(&$array)
{
}
calculateScores($array);
with the desired result:
$array = [
[
'result' => 5,
'weight' => 50,
'children' => [
['result' => 5, 'weight' => 50, 'children' => []],
['result' => 5, 'weight' => 50, 'children' => []]
]
],
[
'result' => 3,
'weight' => 50,
'children' => []
],
];
So, basically. If an entry has children, its score must be calculated by multiplying the results with the respective weights of these.
As the $array could have more than one level, a recursive function could do the job.
However, I have a very hard time to come up with an approach at all. I tried several ideas without any success.