Skip to content

Instantly share code, notes, and snippets.

@LegaiAA
Created April 9, 2018 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LegaiAA/fbbc44959c38c094fa58ef38082042c0 to your computer and use it in GitHub Desktop.
Save LegaiAA/fbbc44959c38c094fa58ef38082042c0 to your computer and use it in GitHub Desktop.
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