简体   繁体   English

PHP数组和ArrayObject

[英]PHP Array and ArrayObject

which one should be used to manipulating data, Array or Array Object? 哪一个应该用来操纵数据,数组或数组对象? Like search,sort and other array manipulations. 像搜索,排序和其他数组操作。

The basic type is array . 基本类型是array This is a map of keys and values that can be written to, read from and accessed in a loop. 这是可以在循环中写入,读取和访问的键和值的映射。

The ArrayObject is a class that you can extend to create objects that behave as if they were arrays. ArrayObject是一个类,您可以扩展它以创建行为就像它们是数组一样的对象。 It implements methods like count and sort that enable you to treat an object like you would treat an array. 它实现了countsort方法,使您可以像处理数组一样处理对象。 It's part of the SPL (Standard PHP Library). 它是SPL(标准PHP库)的一部分。

Typically you'd use array . 通常你会使用array You'll know when you need ArrayObject . 你会知道什么时候需要ArrayObject

In term of performance, you won't notice a real difference between an array and a ArayObject . 在性能方面,您不会注意到arrayArayObject之间的真正区别。 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. 我们的想法是使用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. 平均值是每种方法和每行数的100次测试的平均时间。 The difference between methods is quite insignificant (84 microseconds when you deal with a million rows...) 方法之间的区别是非常微不足道的(当处理一百万行时为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, an array is quite enough, 如果你处理简单的数组,并做一个像foreach()或计算平均值的循环,一个array是足够的,
  • if you need more complex iterations, sorting, filtering, ... it's easier to expand the ArrayObject class with your own iterator, methods... 如果你需要更复杂的迭代,排序,过滤......用你自己的迭代器,方法扩展ArrayObject class更容易......

arrayObject is mostly useful when serialization is needed. 当需要序列化时,arrayObject最有用。

Also you can create your own collection class by extending arrayObject. 您还可以通过扩展arrayObject来创建自己的集合类。 Then you can serialize your class object to transfer data. 然后,您可以序列化您的类对象以传输数据。

for simple and usual operation array is more prefferable than arrayObject. 对于简单和通常的操作,数组比arrayObject更令人满意。

you use array for simple (and standard) arrays 您将array用于简单(和标准)数组

ArrayObject is a class, which can be used to enhance your own Classes to be used as arrays (say some Collection Class of yours) ArrayObject是一个类,可用于增强您自己的类以用作数组(比如你的某些Collection类)

Most of the time an array is all that's needed. 大多数情况下,阵列就是所需要的。 ArrayObject is most useful when extended for functionality in particular uses. 当扩展特定用途的功能时,ArrayObject最有用。

Array Object could be extended and functions overidden. 可以扩展Array对象并覆盖函数。 For example, your append() function could format a number to two decimal places before calling parent::append() 例如,你的append()函数可以在调用parent :: append()之前将数字格式化为两个小数位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM