I am making a combined media feed class that receives Facebook and blogger posts and tweets from Twitter. Then it combines them into one list for display on the site. The problem is that you rarely see any of the message types, because twitter is much more active than the other two.
I managed to display at least one of each type, however, I did this by counting the number of each type in the final array and then splicing them to the end if there were none, and I wonder if there is a more elegant solution for this?
My array contains many arrays, each of which has a type value, this is what I need to check for / to have at least one of them.
before splicing:
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 that he will have only 5 new ones. However, I want him to have five new ones, but make sure that he has at least one with the type “blogger” and one with the type “facebook” in the final array.
Got this 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 quite sure I like it
source share