PHP 数组和 ArrayObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1400404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Array and ArrayObject
提问by Imrul
which one should be used to manipulating data, Array or Array Object? Like search,sort and other array manipulations.
应该用哪一个来操作数据,数组还是数组对象?像搜索、排序和其他数组操作。
回答by drewm
The basic type is array. This is a map of keys and values that can be written to, read from and accessed in a loop.
基本类型是array. 这是可以在循环中写入、读取和访问的键和值的映射。
The ArrayObjectis a class that you can extend to create objects that behave as if they were arrays. It implements methods like countand sortthat enable you to treat an object like you would treat an array. It's part of the SPL (Standard PHP Library).
该ArrayObject是一个类,你可以扩展来创建对象,其行为就好像它们是阵列。它实现了类似count和sort这样的方法,使您能够像对待数组一样对待对象。它是 SPL(标准 PHP 库)的一部分。
Typically you'd use array. You'll know when you need ArrayObject.
通常你会使用array. 你会知道什么时候需要ArrayObject。
回答by Bertrand Fourrier
In term of performance, you won't notice a real difference between an arrayand a ArayObject.
I run a simple test. The idea was to create arrays using array() and new ArrayObject, and fill them with an increasing number of values.
在性能方面,您不会注意到 anarray和 a之间的真正区别ArayObject。我运行了一个简单的测试。这个想法是使用 array() 和 new ArrayObject 创建数组,并用越来越多的值填充它们。
<?php
for($i = 0; $i < 2; $i++){
$method = $i == 0 ? 'array' : 'ArrayObject';
for($j = 0; $j < 7 ; $j++){
for($k = 0; $k < 100; $k++){
$max = pow(10,$j);
$array = $method == 'array' ? array() : new ArrayObject;
$time = explode(' ',microtime());
$sTime = $time[0] + $time[1];
for($l = 0; $l < $max; $l++){
$array[] = 'foo ' . $i . ':' . $j . ':' . $k . ':' . $l;
}
$time = explode(' ',microtime());
$eTime = $time[0] + $time[1];
$results[$method][$max][] = $eTime - $sTime;
}
}
}
?>
Results
结果
method lines average (μs) difference between methods (μs)
array 1 2.470 -1.044
array 10 8.452 +0.315
array 100 71.862 +10.719
array 1,000 773.826 +141.962
array 10,000 7,868.731 -675.359
array 100,000 76,954.625 -17,665.510
array 1,000,000 801,509.550 -84,356.148
ArrayObject 1 3.514 +1.044
ArrayObject 10 8.137 -0.315
ArrayObject 100 61.142 -10.719
ArrayObject 1,000 631.864 -141.962
ArrayObject 10,000 8,544.090 +675.359
ArrayObject 100,000 94,620.135 +17,665.510
ArrayObject 1,000,000 885,865.698 +84,356.148
The average is the average time of the 100 tests for each method and each number of lines. The difference between methods is quite insignificant (84 microseconds when you deal with a million rows...)
平均值是每种方法和每种行数的 100 次测试的平均时间。方法之间的差异非常小(处理一百万行时为 84 微秒......)
I've run this test many times, and because the differences are always a question of microseconds, sometimes a method is more efficient during one test, then less efficient during the next test...
我已经多次运行这个测试,因为差异总是一个微秒的问题,有时一种方法在一次测试中效率更高,然后在下一次测试中效率更低......
The choice will depend of your needs:
选择将取决于您的需求:
- if you deal with simple arrays, and do a loop like
foreach()or calculate an average, anarrayis quite enough, - if you need more complex iterations, sorting, filtering, ... it's easier to expand the
ArrayObject classwith your own iterator, methods...
- 如果你处理简单的数组,并做一个循环
foreach()或计算平均值,一个array就足够了, - 如果您需要更复杂的迭代、排序、过滤……
ArrayObject class使用您自己的迭代器、方法进行扩展会更容易……
回答by Sadat
arrayObject is mostly useful when serialization is needed.
当需要序列化时,arrayObject 最有用。
Also you can create your own collection class by extending arrayObject. Then you can serialize your class object to transfer data.
您也可以通过扩展 arrayObject 创建自己的集合类。然后你可以序列化你的类对象来传输数据。
for simple and usual operation array is more prefferable than arrayObject.
对于简单和常用的操作,数组比数组对象更可取。
回答by knittl
you use arrayfor simple (and standard) arrays
您array用于简单(和标准)数组
ArrayObjectis a class, which can be used to enhance your own Classes to be used as arrays (say some Collection Class of yours)
ArrayObject是一个类,可用于增强您自己的类以用作数组(例如您的某个集合类)
回答by GZipp
Most of the time an array is all that's needed. ArrayObject is most useful when extended for functionality in particular uses.
大多数情况下,只需要一个数组即可。ArrayObject 在针对特定用途的功能进行扩展时最有用。
回答by Anthony Scaife
Array Object could be extended and functions overidden. For example, your append() function could format a number to two decimal places before calling parent::append()
可以扩展数组对象并覆盖函数。例如,您的 append() 函数可以在调用 parent::append() 之前将数字格式化为两位小数

