tags:

views:

90

answers:

4

lets say;

I have a $friends array with 2,000 different friendID numbers

+

I have a $bulletins array with 10,000 bulletinID numbers, the $bulletins array will also have another value with a userID of who posted the bulletin entry

Now is it possible to get all the bulletinID numbers that have a userID matching a userID in the friends array? And if it is even possible, would this be fast or slow or not generally a good method? I am trying to get bulletin type posts on my site and only show ones posted by a friend of a user but some users have a few thousand friends and bulletins could be in the thousands but only some of them a user is allowed to view

Also if this is possible, could I limit it to oly get like the first 50 bulletins ID's that match a friendID

A: 

If you'd post a bit of each array (not all 10,000 items, the first 10 would do) you may get more bites.

Check out array_search() in the meantime.

rpflo
+1  A: 

I'm assuming here that your $friends array is just an array of ints and each item in your $bulletins is an array with userId and some extra fields.

$len = count($bulletins);
$matchedBulletins = array();
for ($i = 0; $i < $len; $i++) {
    if (in_array($bulletins[$i]['userId'], $friends) {
        $matchedBulletins[] = $bulletins[$i];
    }
}

If you wan't to limit this array to like 50 first records only just add a condition inside a loop.

$len = count($bulletins);
$matchedBulletins = array();
$bulletinsCount = 0;
for ($i = 0; $i < $len; $i++) {
    if (in_array($bulletins[$i]['userId'], $friends) {
        $matchedBulletins[] = $bulletins[$i];
        $bulletinsCount++
        if ($bulletinsCount == 50) {
            break;
        }
    }
}
RaYell
+6  A: 

Where are you getting these arrays of thousands of friends/bulletins? If the answer is a relational database (MySQL, PostgreSQL), then this should be done using a SQL query as it is quite trivial, and much more efficient than anything you could do in PHP.

Here is an example of how this could be done in SQL:

SELECT
  posts.id
FROM posts
JOIN users ON posts.user_id = users.id
JOIN user_friends ON user_friends.user_id = users.id
WHERE posts.type = 'bulletin'
AND user_friends.user_id = 7
LIMIT 50;

Obviously it is done with no knowledge of your actual database structure, if any, and thus will not work as-is, but should get you on the right path.

hobodave
My first thoughts exactly ...
rpflo
the reason for look at other methods is because the friends array comes from a cache
jasondavis
Do the bulletins come from a cache as well?
hobodave
The cache is still irrelevant if this is a SQL backed application. You're killing your performance by iterating over thousands of elements when you don't need to. This can still be retrieved using a SQL query.
hobodave
+1  A: 

Ok, so it sounds like you have an array of friend ids that is not associative, ie array(0 => 'userid0', 1 => 'userid1', etc), and an array of bulletin ids that is associative, ie. array('bulletin1' => 'userid1', 'bulletin2' => 'userid2', etc).

Going with that assumption, you can get all the matching bulletins using array_intersect(). You can then take the first fifty bulletin keys with array_slice():

$matchingBulletins = array_intersect($bulletins, $friends);
$first50 = array_slice(array_keys($matchingBulletins),0,50);

It sounds like you might be getting this data out of a database however, in which case it would be much more prudent to filter your database results somehow and avoid returning 10,000 ids each time. You could do the sorting and filtering using JOINs and WHEREs on the right tables.

zombat
thank you, everyone is probably right about using the DB to get the results but I am caching the friendlist array so I am looking at all possible ways of doing it and I think the best way is to test every method
jasondavis