使用SPL ArrayObject,ArrayIterator,RecursiveArrayIterator而不是常规数组有什么好处? [英] What are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays?

查看:150
本文介绍了使用SPL ArrayObject,ArrayIterator,RecursiveArrayIterator而不是常规数组有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始从ArrayIterators学习PHP SPL了,我想知道使用SPL ArrayObject,ArrayIterator,RecursiveArrayIterator而不是常规数组有什么好处?

I've started to learn PHP SPL from ArrayIterators and I'd like to know what are the benefits of using SPL ArrayObject, ArrayIterator, RecursiveArrayIterator instead of regular arrays?

a)我听说使用SPL迭代器的循环会减少内存使用量(但为什么呢?)。我真的不知道相信这个或不相信,因为我不明白它如何减少内存使用。

a) I've heard that loops using SPL iterators will reduce memory usage (but why?). I don't really know to believe this or no because I don't understand how can it reduce memory usage.

b)谈论RecursiveArrayIterator我们可以说有时它可以保存一些代码行(我们使用一个foreach构造而不是2+(取决于数组维度))。

b) Talking about RecursiveArrayIterator we can say that sometimes it could save some lines of code (we're using one foreach construction instead of 2+ (depends on array dimension)).

可能我的问题似乎很对某些人来说很容易,但关于SPL的信息/文档太少了。

Probably, my questions could seem to be very easy for somebody, but there are too little information/documentation about SPL.

谢谢

推荐答案

您正在寻找有效数据访问的主要好处,主要是在可读性方面。通过这种方式,您可以像使用foreach中的数组一样使用对象,以便与PHP进行更密切的交互。

The primary benefit you're looking at efficient data access mostly in the area of readability. This way you can use object just like an array in a foreach so a closer interaction with PHP.

a)减少内存使用量的方法是不复制数组,但如果在引擎中正确完成,那么它应该是一个新的引用,直到数组被修改然后它实际上被复制。

a) The way you reduce memory usage is by not making a copy of the array but if done properly in the engine then it should be a new reference until the array is modified then it's actually copied.

b)基本上

这方面的一个很好的例子是 SimpleXML 扩展。您正在与之交互的PHP / XML对象也像一个数组,它使得它的使用效率很高,因为您可以只访问或访问列表中的一个元素。您不必从对象中获取数组,然后如果需要标记名称或属​​性,则迭代并使用它执行其他操作。

A good example of this would be the SimpleXML extension. The PHP/XML object you're interacting with also acts like an array which makes it efficient to use because you can just foreach or access one of the elements within the list. You don't have to grab an array from the object and then iterate over and do something else with it if you want the tag name or an attribute.

这篇关于使用SPL ArrayObject,ArrayIterator,RecursiveArrayIterator而不是常规数组有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆