PHP:修改多维数组的最简洁方法?
我的应用程序中有一个 Config
类,它加载静态配置设置并将它们解析为数组。
由于我需要在运行时覆盖一些元素,因此我需要通过这样做来访问 Config 类内的公共变量; $config->values['onelevel']['twolevel'] = 'changed';
我想创建一个名为 override
的方法来执行此操作我,但我无法弄清楚最好的方法是什么,因为我的配置文件将来可能会获得未知数量的嵌套级别。
做类似 $config->onelevel->twolevel = 'changed'
并让 __set 魔术方法处理嵌套的事情会很高兴,但据我所知,它不是不可能的。
最好的方法是什么?
I've got a Config
class in my application, which loads static config settings and parses them into arrays.
As I need to override some elements during runtime, I would need to access the public variable inside the Config
-class by doing this; $config->values['onelevel']['twolevel'] = 'changed';
I would like to make a method that is called override
that does this for me, but I cant get my head around what would be the best way to do it as my config files may get unknown amount of nested levels in the future.
It would be lovely to do something like $config->onelevel->twolevel = 'changed'
and let the __set magic method take care of the nesting, but from what I can tell, it isn't possible.
What would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
做你想做的事是可能的。
这个例子很大程度上受到 Zend_Config 以及 ArrayAccess 接口。
编辑:
有一个小警告:您需要对表示数组的数据调用进行访问toArray()
,将其转换为数组,因为类内部需要将数组数据转换为自身的实例,允许使用对象属性运算符->
:呃,当然,这不再是必要的了,因为它现在实现了 ArrayAccess。 ;-)
/编辑
编辑2:
甚至可以通过扩展 ArrayObject 来极大地简化 Config 类。作为一个额外的好处,您还可以将其转换为适当的数组。
用法示例:
It is possible to do what you want.
This example is largly inspired by Zend_Config and the example given in the PHP docs on the ArrayAccess interface.
edit:
With one minor caveat: you need to calltoArray()
on data representing an array, to convert it to an array, as the class internally needs to covert array data to an instance of itself, to allow access with the object property operator->
:Eh, that's not really necessary anymore of course, since it implements ArrayAccess now. ;-)
/edit
edit 2:
The
Config
class can even be greatly simplified by extendingArrayObject
. As an added benefit, you can cast it to a proper array also.Example usage:
我也遇到过这个问题,我用这段代码解决了这个问题。然而,它基于 API,例如:
Config::set('paths.command.default.foo.bar')
。它只是循环遍历数组并使用引用变量跟踪当前值。
I too have had this problem, and I solved this with this code. However it was based on API like:
Config::set('paths.command.default.foo.bar')
.It is just looping through the array and holding track of the current value with a reference variable.
我会创建一个具有无限数量参数的函数,并使用
func_get_args()
获取参数,从那里,它只是更新。I'd make a function with an indefinite amount of arguments, and use
func_get_args()
to get the arguments, from there, it's simply updating.好吧,你说你将它们解析成数组。为什么不将它们解析为
stdObjects
,然后简单地按照您的意愿执行$config->onelevel->twolevel = 'changed'
?:)Well, you said you parse them into array. Why not parse them into
stdObjects
and then simply do$config->onelevel->twolevel = 'changed'
as you want?:)您可以自己构建一个类型,提供您正在寻找的接口,或者使用您描述的辅助函数。
这是覆盖函数演示的代码示例:
You could either build a type on your own, that is providing the interface you're looking for or you go with the helper function you describe.
This is a code example of an override function Demo:
前段时间我需要一个函数,让我通过字符串路径访问数组,也许你可以利用它:
示例:
PMA_array_write('onelevel/twolevel', $array, 'value');
Some time ago I needed a function that would let me access an array through string path, maybe you can make use of that:
Example:
PMA_array_write('onelevel/twolevel', $array, 'value');