PHP Array and ArrayObject
Asked Answered
S

6

36

which one should be used to manipulating data, Array or Array Object? Like search,sort and other array manipulations.

Selfappointed answered 9/9, 2009 at 15:28 Comment(2)
Do you have an example of what you want to do?Prosaism
I need to search,sort and other complicated manipulation using Array basically for using it in algorithm. So i need to know which one is better and faster.Selfappointed
D
40

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.

The ArrayObject is a class that you can extend to create objects that behave as if they were arrays. It implements methods like count and sort that enable you to treat an object like you would treat an array. It's part of the SPL (Standard PHP Library).

Typically you'd use array. You'll know when you need ArrayObject.

Dunkin answered 9/9, 2009 at 15:36 Comment(0)
R
9

In term of performance, you won't notice a real difference between an array and 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.

<?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...)

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,
  • if you need more complex iterations, sorting, filtering, ... it's easier to expand the ArrayObject class with your own iterator, methods...
Rugger answered 25/11, 2014 at 14:13 Comment(2)
Please, specify version of php, it's quite significant.Anemia
Looks to me like the difference is 84 milliseconds (84 365 microseconds) when you deal with a million rows... not that the difference is significant in most cases.Tremml
F
3

arrayObject is mostly useful when serialization is needed.

Also you can create your own collection class by extending arrayObject. Then you can serialize your class object to transfer data.

for simple and usual operation array is more prefferable than arrayObject.

France answered 9/9, 2009 at 17:31 Comment(0)
C
1

you use array for simple (and standard) arrays

ArrayObject is a class, which can be used to enhance your own Classes to be used as arrays (say some Collection Class of yours)

Cervelat answered 9/9, 2009 at 15:40 Comment(1)
ArrayObject isn't an abstract class.Foil
D
0

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()

Deas answered 9/9, 2009 at 15:28 Comment(0)
F
0

Most of the time an array is all that's needed. ArrayObject is most useful when extended for functionality in particular uses.

Foil answered 9/9, 2009 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.