在 PHP 中存储值
我试图在 php 中存储值,就像具有多个键的哈希表一样。例如,我希望它返回两个不同的值:
$value1=$content['var1']['var2']['var3']['typea'];
$value2=$content['var1']['var2']['var3']['typeb'];
实现这样的功能的最佳方法是什么?
I am trying to store values in php, like a HashTable with multiple keys. For example I am I would want this to return two different values:
$value1=$content['var1']['var2']['var3']['typea'];
$value2=$content['var1']['var2']['var3']['typeb'];
What would be the best way to go about implementing a feature like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与其构建复杂的数组,不如定义并使用一个简单的类怎么样?例如:
Instead of building a complicated array, how about you define and use a simple class instead? e.g:
您可以按照获取值的方式设置值。
You can set values the same way you get them.
您可以利用 PHP 作为动态脚本语言的优势,让它自动创建您想要的数组结构:
You can take advantage of PHP being a dynamic scripting language by letting it create your desired array structure automatically:
一种方法是根据各种密钥创建一个唯一的密钥并将其存储在一个大数组中。
相反,
你会得到类似这样的东西...
其中
generateKey
会执行诸如散列各种输入之类的操作,或者将它们与一些唯一的分隔符(例如四重下划线)连接在一起。与其他解决方案相比,这需要更少的数组查找,并且(在我看来)更易于阅读。One way would be to make a unique key based on the various keys and store it in one big array.
Instead of this,
you'd have something like this...
where
generateKey
would do something like hash the various inputs, or concatenate them together with some unique delimiter like a quadruple underscore. This would require fewer array lookups than the other solutions, and (in my opinion) is easier to read.