Skip to content

Instantly share code, notes, and snippets.

@n00shie
Last active August 29, 2015 14:11
Show Gist options
  • Save n00shie/d6c83726a9a90771c49b to your computer and use it in GitHub Desktop.
Save n00shie/d6c83726a9a90771c49b to your computer and use it in GitHub Desktop.
$toData = $datum['to']['data'];
// rekey to use user id's as index
$rekeyedToData = [];
foreach($toData as $toUser) {
$key = $toUser['id'];
// add an extra 'count' key for user ids that show up more than once
if(array_key_exists($key, $rekeyedToData)) {
$rekeyedToData[$key]['count']++;
} else {
$rekeyedToData[$key] = $toUser;
$rekeyedToData[$key]['count'] = 1;
}
}
// iterate through message_tags, remove the tagged user from $rekeyedToData
// if the same id showed up more than once, decrement it
if(isset($datum['message_tags'])) {
foreach($datum['message_tags'] as $tag) {
// each tag object in FB's JSON response is actually wrapped in an array.
// if FB decides to change this structure in the future, then the following line will break.
$key = $tag[0]['id'];
if($rekeyedToData[$key]['count'] > 1) {
$rekeyedToData[$key]['count']--;
} else {
$rekeyedToData[$key] = null;
}
}
}
// do the same with with_tags, remove the tagged user from $rekeyedToData
$rekeyedToData = array_filter($rekeyedToData);
// if $rekeyedToData is empty, then this post was a self post, e.g the poster posted on their own wall and tagged a bunch of people
// otherwise the post has a definite recipient
if(!empty($rekeyedToData)) {
// should never be more than one element in the array
if(count($rekeyedToData) > 1){
Logger::log("More than one element in recipient array.");
}
$recipient = end($rekeyedToData); // could have used reset, instead of end to get the first element. Assumption is there would only by one element in the array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment