Reputation: 48963
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
Upvotes: 2
Views: 139
Reputation: 29303
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.
Upvotes: 6
Reputation: 94207
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 JOIN
s and WHERE
s on the right tables.
Upvotes: 1
Reputation: 70424
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;
}
}
}
Upvotes: 1
Reputation: 13483
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.
Upvotes: -1