Remove duplicate pairs from Facebook FQL result in PHP
I'm using the following Facebook FQL query to fetch the relationships between my friends:
SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1 = $myUid) 开发者_如何学编程AND uid2 IN (SELECT uid2 FROM friend WHERE uid1 = $myUid)
The result is a two-column table of undirected relations between my friends, e.g.:
UID1 UID2
UID2 UID3
UID2 UID1
UID3 UID5
In the above example, the relationship (UID1,UID2) appears twice, one as (UID1,UID2) and once as (UID2,UID1). Both relationships are equivalent. I wish to remove such duplicates leaving only one of them.
So, using PHP, what is the best way to remove such duplicates? Thanks for any ideas or pointers.
Best, Andrej
Simple.
Add
AND uid1 < uid2
to your query.
If it MUST be in PHP and your result set is not too long:
class UniqueChecker {
private $aMap = array();
public function mustAddPair($sItem1, $sItem2) {
if (empty($this->aMap["$sItem1 $sItem2"]) && empty($this->aMap["$sItem2 $sItem1"])) {
$this->aMap["$sItem1 $sItem2"] = true;
return true;
}
else
return false;
}
}
精彩评论