views:

64

answers:

4

I have a function which must return one dimensional associate array, like $user_info[$index]=value where $index is a string which consist of

  • user_id
  • full_name
  • photo_file_name

for example, my associative array could look like $user_info['user-123456789~~Bill Gates~~bill_gates.png']=$value. I need user_id, full_name and photo for another needs, in order to know whose value is this and what is his full name, etc.
So, the questions that has risen are following:

  1. Is it OK to have such an array if to take into account performance of the application?
  2. If it is bad (I think that it is bad idea) so how can I solve my problem in this case.

Additional info. This function retrieves user information into this associative array and returns this array. Further, my application stores it in a session in order to address it and retrieve information right from the session variable, not execute once again a query. And finally the reason I need one dimensional array is that I use a function array_diff where one of the arrays are $user_info array.
Note, Take into account that one user could have 1 or many values.

Any suggestions would be pleased.

+3  A: 

You have a long key in an associative array.

I don't think it's a problem. If you wanted to shorten it you could hash your key-value and store the value with the hashed value.

That being said, have you asked yourself why you're using that schema? Might it be better to come up with a different data structure to suit your needs?

I'd be much more concerned with the amount of data (size of the array) that you're storing in the session variable. I think using the session to store data like this is a bad idea.

mmattax
+1 for pointing out session bottleneck. Note that sessions in PHP usually is stored as a serialized array into a file on disk. Serialization and unserialization of big arrays use up A LOT of CPU time, and the extra disk work for the session can be a bottleneck too.
Emil Vikström
+1. You are probably right. I need to change the structure of data storage I have.
Bakhtiyor
@mmattax. Do you know what is the problem if I don't save this information in the session? I need to perform query to DB every time user refresh the page. Besides, I need to perform calculation on that retrieved data. But with the session I do it only once and further if session is set I just print it. Any idea about how to solve this problem?
Bakhtiyor
@Bakhtiyor Are you using the session data on multiple pages or just one? I'd fetch the data from the DB each time the page loads. What you describe is caching and (imo) a session is not the place to do that.
mmattax
@mmattax. The problem actually is not in a fetching data from DB. But I need to do some mathematical complex operations with this data and them show part of them (the result). I use it only in a one page.
Bakhtiyor
+2  A: 

Any performance gained by concatenating that much information into a string is easily lost when you consider the maintenance/readabilty costs. Will that be easy to understand when the next programmer comes along and starts reading your code?

I would suggest an alternate data structure:

$user_info[$user_id] = array(
  'user_id'         => $user_id, // Redundant but helpful
  'full_name'       => $full_name,
  'photo_file_name' => $photo,
  'value'           => $value
);
Mike B
You are right, therefore I am looking for a solution.
Bakhtiyor
But take into account that one user could have 1 or many values.
Bakhtiyor
Since the OP mentioned he wants to `array_diff` the array, you could `md5` the keys the OP used for a compound key. Then the OP could `array_diff_key` the arrays.
Gordon
A: 

So you are storing information (more than a key) into ~-separated array keys? It looks really ugly. It's probably not a performance problem though*.

Is it impossible to reimplement array_diff but modified for your needs?

*) I looked in the manual for information about how the keys are handled (balanced b-trees maybe?), but couldn't find anything satisfactory. There could actually possibly be a performance problem depending on the implementation, but PHP is usually good at this type of work.

Emil Vikström
Hmmm, that was just a quick realization of the problem and I didn't think about how ugly that was. But know I am concerned about it.
Bakhtiyor
A: 

On a point of terminology: in your example, the name of the associative array is $user_info. 'user-123456789~~Bill Gates~~bill_gates.png' is a key of the array. Arrays in PHP have keys and values.

To answer your question: No, there is nothing in particular wrong with having associative arrays whose keys are long strings; PHP doesn't particularly care. (The example you gave is not really a "long" string, anyway.)

However, the way you are doing things, as described, does sound like it could be improved. Consider adopting the array structure suggested by Mike B, or implementing a "user" class and returning an array of users from your function.

Hammerite
Yes, but I didn't mentioned it in a question but one user could have 1 or more values. I updated the question. Sorry for that.
Bakhtiyor