which one should be used to manipulating data, Array or Array Object? Like search,sort and other array manipulations.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
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.
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 likecount
andsort
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 needArrayObject
.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()
Most of the time an array is all that's needed. ArrayObject is most useful when extended for functionality in particular uses.
In term of performance, you won't notice a real difference between an
array
and aArayObject
. 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.Results
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:
foreach()
or calculate an average, anarray
is quite enough,ArrayObject class
with your own iterator, methods...you use
array
for simple (and standard) arraysArrayObject
is a class, which can be used to enhance your own Classes to be used as arrays (say some Collection Class of yours)