PHP 中关联数组的名称太长是不是不好?
组成的字符串,
- 我有一个必须返回一维关联数组的函数,例如
$user_info[$index]=value
其中$index
是一个由user_id - full_name
- photo_file_name
例如,我的关联数组可能类似于 $user_info['user-123456789~~Bill Gates~~bill_gates.png'
]=$value。我需要 user_id、full_name 和 photo 来满足另一个需求,以便知道这个值是谁以及他的全名是什么等等
那么,出现的问题如下:
- 如果考虑到应用程序的性能,可以使用这样的数组吗?
- 如果它很糟糕(我认为这是个坏主意)那么在这种情况下我该如何解决我的问题。
其他信息。该函数将用户信息检索到该关联数组中并返回该数组。此外,我的应用程序将其存储在会话中,以便对其进行寻址并直接从会话变量中检索信息,而不是再次执行查询。最后,我需要一维数组的原因是我使用了一个函数 array_diff
,其中数组之一是 $user_info
数组。
请注意,请考虑到一个用户可能有 1 个或多个值。
任何建议都会很高兴。
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:
- Is it OK to have such an array if to take into account performance of the application?
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关联数组中有一个长键。
我认为这不是问题。如果您想缩短它,您可以散列您的键值并将该值与散列值一起存储。
话虽这么说,您是否问过自己为什么要使用该架构?提出不同的数据结构来满足您的需求可能会更好吗?
我更关心您存储在会话变量中的数据量(数组的大小)。我认为使用会话来存储这样的数据是一个坏主意。
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.
当您考虑维护/可读性成本时,通过将大量信息连接到字符串中所获得的任何性能都很容易丢失。当下一个程序员出现并开始阅读您的代码时,这会容易理解吗?
我建议使用替代数据结构:
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:
所以您将信息(不仅仅是一个键)存储到 ~ 分隔的数组键中?看起来真的很难看。但这可能不是性能问题*。
是否不可能重新实现 array_diff 但根据您的需要进行修改?
**)我在手册中查找了有关如何处理键的信息(也许是平衡的 B 树?),但找不到任何令人满意的内容。根据实现的不同,实际上可能会出现性能问题,但 PHP 通常擅长此类工作。*
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.*
关于术语:在您的示例中,关联数组的名称是
$user_info
。'user-123456789~~Bill Gates~~bill_gates.png'
是数组的键。 PHP 中的数组有键和值。回答你的问题:不,拥有键为长字符串的关联数组并没有什么特别的问题; PHP 并不特别关心。 (无论如何,您给出的示例实际上并不是一个“长”字符串。)
但是,正如所描述的,您做事的方式听起来确实可以改进。考虑采用 Mike B 建议的数组结构,或实现“用户”类并从函数返回用户数组。
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.