PHP custom function code optimization
- by Alex
Now comes the hard part. How do you optimize this function:
function coin_matrix($test, $revs) {
$coin = array();
for ($i = 0; $i < count($test); $i++) {
foreach ($revs as $j => $rev) {
foreach ($revs as $k => $rev) {
if ($j != $k &&
$test[$i][$j] != null &&
$test[$i][$k] != null) {
if(!isset($coin[$test[$i][$j]])) {
$coin[$test[$i][$j]] = array();
}
if(!isset($coin[$test[$i][$j]][$test[$i][$k]])) {
$coin[$test[$i][$j]][$test[$i][$k]] = 0;
}
$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
}
}
}
}
return $coin;
}
I'm not that good at this and if the arrays are large, it runs forever.
The function is supposed to find all pairs of values from a two-dim array and sum them like this: $coin[$i][$j] += sum_of_pairs_in_array_row / [count(elements_of_row) - 1]
Thanks a lot!