Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Seperate single tweets into an individual collection. Combine threaded Tweets / Tweet replies into combined collection.
<?php
/**
* Seperate single tweets into an individual collection.
* Combine threaded Tweets / Tweet replies into combined collection.
*/
function get_tweets($data)
{
if ($data === false) {
return 'Cannot view tweets';
}
//array contaning tweet data
$tweets = [];
//replied tweet id
$rid = null;
//index increment to group tweets
$idx = 1;
//loop over tweets
foreach ($data as $key => $tw) {
//Successive tweets in thread
if (!is_null($tw->in_reply_to_status_id) && is_null($rid)) {
$tweets[$idx][$tw->id] = $tw->full_text;
continue;
}
//First tweet in thread
if (is_null($tw->in_reply_to_status_id) && is_null($rid)) {
$tweets[$idx][$tw->id] = $tw->full_text;
}
//Store replied tweet id
$rid = $tw->in_reply_to_status_id;
//Index increment to group tweets
$idx++;
}
//return tweets
return $tweets;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.