在 PHP 中使用 array_search 使对象可搜索
我想在 PHP 中创建一个类,以便可以使用 PHP 本机方法 array_search
进行搜索。目前,我的类实现了 IteratorAggregate 和 Countable,这允许我对其执行 foreach 操作。
还有几个其他 SPL 接口(SeekableIterator、ArrayAccess 等)似乎可能合适,但我想知道是否有人已经这样做了,以及哪个接口适合我想做的事情。
谢谢
I would like to make a class in PHP such as it would be searchable with PHP native method array_search
. Currently my class implements IteratorAggregate
and Countable
, which allows me to do foreach
on it.
There is a couple of other SPL interfaces (SeekableIterator, ArrayAccess, etc...) which seems to maybe fit, but I would like to know if someone has already done this and which interface is suitable for what I want to do.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 FilterIterator。您实现继承自它的自己的类,然后在类中的
accept
方法实现中进行比较。Check out FilterIterator. You implement your own class inheriting from it then do a compare in the
accept
method implementation in your class.如果可能的话,让您的类扩展
ArrayObject
。如果不可能,请尝试基于 本手册了解您的对象真正需要使用哪个接口。Make your class extend
ArrayObject
, if possible. If that's not possible, try implementing each of the interfaces that ArrayObject implements, one-by-one based on this manual to see which interface you really need to use for your object.