• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

php in_array efficiency

Hello,

I'm a little concerned over computing cost of a feature that I hope some people can give me reassurance :)

I have a sphinx search on documents with some features to both 'favorite' and 'mark as read' on each result returned.

These features which are available require a store of all items marked as either favorited or mark as read.

Favorited max cap is 2000 and mark as read can go 2000+

My theory is to store all the doc_ids in 2 arrays upon login. So my first thought is, is that dangerous to store so many values in a $_SESSION?

Next thought is when the results are returned, I need to run something like the below:

if(in_array($doc_id, $_SESSION['favorites_array']))
{
	$favorite_link = 'Favorited';
}
else
{
	$favorite_link = '<a href="addtofavorites.php">Add to Favorites</a>';
}

if(in_array($doc_id, $_SESSION['read_array']))
{
	$read_link = 'Marked as Read';
}
else
{
	$read_link = '<a href="addtofavorites.php">Mark as Read</a>';
}

Open in new window


Just need some expert opinion really on the best way of achieving this - trying to avoid db queries where I can

$doc_id's are INTs

Cheers
0
dolythgoe
Asked:
dolythgoe
  • 2
  • 2
3 Solutions
 
amiguraCommented:
 how the way your doing it is fine. there is no real danger

you could also cache the search results so it reusable
0
 
Chris HarteThaumaturgeCommented:
Also, there is no limit on the number of session variables you can use. It is the number of users starting sessions that will create a bottle neck. With the amount of memory available to most computers these days you will need an awful lot of them to clog your computer.
0
 
dolythgoeAuthor Commented:
Good to hear :) - and the in_array() processing?

There's 24 results per page so php has to either lookup per result or there might be a way of getting the 24 id's and compare array against array to produce an array of matches if you see what I mean. Then compare the much smaller array of matches with each listing...does that make sense?
0
 
Chris HarteThaumaturgeCommented:
array_intersect returns an array of overlapping values

http://uk3.php.net/manual/en/function.array-intersect.php

There are also functions for overlapping keys
0
 
dolythgoeAuthor Commented:
Awesome, gotta love php's ability to have a function for everything!

Cheers guys
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now