I have a results set based on a query from two separate tables. I've combined the results into one array. The results themselves are search results from forums. When I display the results I only want one result per PostID, but I want the sum of the score for all results for a given PostID so the results can be sorted by relevancy before being displayed.
I've got as far as this but can't get any further:
Code: Select all
foreach ($forum_results as $x => $array) {
$nextarray = next($forum_results); //get the next sub-array
if (!isset($key)){ //If $key is already set we must have a match so don't want to advance it
$key=$x; //get key for current position in $forum_results array so we can add the scores correclty
}
if ($array['PostID'] == $nextarray['PostID']){
$forum_results[$key]['Score'] = $array['Score'] + $nextarray['Score']; //add the score to the original array
$x++; //increment $x to delete the next value ($nextvalue)
unset ($forum_results[$x]);
reset ($forum_results);
} else {
unset ($key); //if there is no match then we want $key to point to next new PostID
}
}Code: Select all
Array
(
ї0] => Array
(
їPostID] => 53
їTitle] => blah
їScore] => 288
)
ї1] => Array
(
їPostID] => 53
їScore] => 64
їTitle] => blah
)
ї2] => Array
(
їPostID] => 53
їScore] => 64
їTitle] => blah
)
ї3] => Array
(
їPostID] => 53
їScore] => 64
їTitle] => blah
)
ї4] => Array
(
їPostID] => 53
їScore] => 64
їTitle] => blah
)
ї5] => Array
(
їPostID] => 53
їScore] => 480
їTitle] => blah
)
ї6] => Array
(
їPostID] => 53
їScore] => 64
їTitle] => blah
)
ї7] => Array
(
їPostID] => 56
їScore] => 160
їTitle] => testing
)
ї8] => Array
(
їPostID] => 57
їTitle] => test
їScore] => 192
)
ї9] => Array
(
їPostID] => 57
їScore] => 240
їTitle] => test
)
ї10] => Array
(
їPostID] => 57
їScore] => 192
їTitle] => test
)
)Code: Select all
Array
(
ї0] => Array
(
їPostID] => 53
їTitle] => blah
їScore] => 544
)
ї7] => Array
(
їPostID] => 56
їScore] => 160
їTitle] => testing
)
ї8] => Array
(
їPostID] => 57
їTitle] => test
їScore] => 432
)
ї10] => Array
(
їPostID] => 57
їScore] => 624
їTitle] => test
)
)