ArrayIterator,ArrayObject的和数组在PHP中的区别 [英] Difference between ArrayIterator, ArrayObject and Array in PHP

查看:1374
本文介绍了ArrayIterator,ArrayObject的和数组在PHP中的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释清楚的根本 ArrayIterator ArrayObject的和数组在PHP中的功能和操作方面的差异?谢谢!

Can somebody explain clearly the fundamental differences between ArrayIterator, ArrayObject and Array in PHP in terms of functionality and operation? Thanks!

推荐答案

阵列 PHP原生类型。您可以使用PHP语言结构创建一个阵列(),或作为PHP 5.4起的 []

Array is a native php type. You can create one using the php language construct array(), or as of php 5.4 onwards []

ArrayObject的对象的工作完全一样的阵列。这些可以通过创建关键字

ArrayObject is an object that work exactly like arrays. These can be created using new keyword

ArrayIterator ArrayObject的但它可以在自身循环。还利用创建的

ArrayIterator is like ArrayObject but it can iterate on itself. Also created using new

比较阵列 VS( ArrayObject的 / ArrayIterator

Comparing Array vs (ArrayObject/ArrayIterator)

他们都可以使用PHP数组的语法被使用,如:

They both can be used using the php's array syntax, for eg.

$array[] = 'foo';
$object[] = 'foo';
// adds new element with the expected numeric key

$array['bar'] = 'foo';
$object['bar'] = 'foo';
// adds new element with the key "bar"

foreach($array as $value);
foreach($object as $value);
// iterating over the elements

然而,他们仍然反对VS阵列,所以你会发现在不同

However, they are still objects vs arrays, so you would notice the differences in

is_array($array); // true
is_array($object); // false

is_object($array); // false
is_object($object); // true

大部分的PHP数组函数都阵列,所以使用对象也就抛出错误。有很多这样的功能。对于如。

Most of the php array functions expect arrays, so using objects there would throw errors. There are many such functions. For eg.

sort($array); // works as expected
sort($object); // Warning: sort() expects parameter 1 to be array, object given in ......

最后,对象可以做你期望从 stdClass的的对象是什么,即访问使用对象语法公共属性。

Finally, objects can do what you would expect from a stdClass object, i.e. accessing public properties using the object syntax

$object->foo = 'bar'; // works
$array->foo = 'bar'; // Warning: Attempt to assign property of non-object in ....

阵列(即原生型)比对象快得多。在另一边, ArrayObject的&安培; ArrayIterator 类有一定的方法来定义的,你可以使用,同时有数组没有这样的事情。

Arrays (being the native type) are much faster than objects. On the other side, the ArrayObject & ArrayIterator classes have certain methods defined that you can use, while there is no such thing for arrays

比较 ArrayObject的 VS ArrayIterator

Comparing ArrayObject vs ArrayIterator

这些2之间的主要区别是在类具有方法

The main difference between these 2 is in the methods the classes have.

ArrayIterator 工具的Iterator 接口,这使得它与迭代方法/遍历的元素。 ArrayObject的有一个名为 exchangeArray 方法交换它的内部数组用另一个。在 ArrayIterator 实施了类似的事情将意味着无论是创建一个新对象,或通过按键和放循环; 取消设置由一个与放大器ING所有的人之一;然后设置新阵列中的一个接一个的元素。

The ArrayIterator implements Iterator interface which gives it methods related to iteration/looping over the elements. ArrayObject has a method called exchangeArray that swaps it's internal array with another one. Implementing a similar thing in ArrayIterator would mean either creating a new object or looping through the keys & unseting all of them one by one & then setting the elements from new array one-by-one.

接下来,由于 ArrayObject的不能重复,当你在的foreach 使用它创建一个 ArrayIterator 对象(同数组)。这意味着PHP创建原始数据和放大器的副本;现在有两个对象具有相同的内容。这将被证明是低效的大型阵列。但是,您可以指定要使用的类迭代器,这样你就可以定制迭代器在code。

Next, since the ArrayObject cannot be iterated, when you use it in foreach it creates an ArrayIterator object internally(same as arrays). This means php creates a copy of the original data & there are now 2 objects with same contents. This will prove to be inefficient for large arrays. However, you can specify which class to use for iterator, so you can have custom iterators in your code.

希望这是有帮助的。编辑这个答案,欢迎。

Hope this is helpful. Edits to this answer are welcome.

这篇关于ArrayIterator,ArrayObject的和数组在PHP中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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