I've been making a combined social media feed class which gets posts from Facebook and blogger and tweets from Twitter. It then combines them into one list for display on a site. The problem is that it's rare to see either of the post types because twitter is a lot more active than the other two.
I managed to make it always display at least one of each type, however I did this by counting up the number of each type in the final array and then splicing them onto the end if there were none and am wondering if there is a more elegant solution to this?
My array contains a bunch of arrays which each have a 'type' value, this is the what I need to test for / have at least one of each.
before splice:
Array
(
[0] => Array
(
[id] => 131403235838803968
[from] => foo
[sent] => 1320163947
[type] => tweet
[html] => bar
)
[1] => Array
(
[id] => 131403233250914304
[from] => foo
[sent] => 1320163946
[type] => tweet
[html] => bar
)
[2] => Array
(
[id] => 131403232835674113
[from] => foo
[sent] => 1320163946
[type] => tweet
[html] => bar
)
[3] => Array
(
[id] => 131403230910480384
[from] => foo
[sent] => 1320163946
[type] => tweet
[html] => bar
)
[4] => Array
(
[id] => 131403228834299904
[from] => foo
[sent] => 1320163945
[type] => tweet
[html] => bar
)
[5] => Array
(
[type] => facebook
[from] => foo
[html] => bar
[sent] => 1320065996
)
[6] => Array
(
[type] => facebook
[from] => foo
[html] => bar
[sent] => 1319808945
)
[7] => Array
(
[type] => facebook
[from] => foo
[html] => bar
[sent] => 1319789640
)
[8] => Array
(
[type] => facebook
[from] => foo
[html] => bar
[sent] => 1319707799
)
[9] => Array
(
[type] => facebook
[from] => foo
[html] => bar
[sent] => 1319617295
)
[10] => Array
(
[type] => blogger
[from] => foo
[html] => bar
[sent] => 1320157500
)
[11] => Array
(
[type] => blogger
[from] => foo
[html] => bar
[sent] => 1320148260
)
)
and after it will just have the 5 newest. However I want it to have the five newest but make sure that it has at least one with type 'blogger' and one with type 'facebook' in the final array.
Got it work using Johnny Craig's idea and the following code:
$output = array();
$output[] = $tweets[0];
$output[] = $items[0];
$output[] = $posts[0];
$feed = array_merge($tweets, $items, $posts);
$i = 0;
while ($limit > count($output)) {
if (!in_array($feed[$i], $output)) {
$output[] = $feed[$i];
}
$i++;
}
But not really sure I like it