使用PHP中的array_search使对象可搜索

时间:2022-09-12 22:31:20

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.

我想在PHP中创建一个类,例如可以使用PHP本机方法array_search进行搜索。目前我的类实现了IteratorAggregate和Countable,它允许我对它进行操作。

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.

有几个其他SPL接口(SeekableIterator,ArrayAccess等等)似乎可能适合,但我想知道是否有人已经完成了这个以及哪个接口适合我想要做的事情。

Thank you

2 个解决方案

#1


2  

Check out FilterIterator. You implement your own class inheriting from it then do a compare in the accept method implementation in your class.

查看FilterIterator。您实现了自己继承的类,然后在类的accept方法实现中进行比较。

#2


1  

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.

如果可能,使您的类扩展ArrayObject。如果那是不可能的,请尝试实现ArrayObject实现的每个接口,基于本手册逐个实现,以查看您确实需要为对象使用哪个接口。

#1


2  

Check out FilterIterator. You implement your own class inheriting from it then do a compare in the accept method implementation in your class.

查看FilterIterator。您实现了自己继承的类,然后在类的accept方法实现中进行比较。

#2


1  

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.

如果可能,使您的类扩展ArrayObject。如果那是不可能的,请尝试实现ArrayObject实现的每个接口,基于本手册逐个实现,以查看您确实需要为对象使用哪个接口。