各种类及接口
在线手册:中文  英文

The ArrayObject class

(PHP 5)

简介

This class allows objects to work as arrays.

类摘要

ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable {
/* 常量 */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* 方法 */
public __construct ([ mixed $input [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )
public void append ( mixed $value )
public void asort ( void )
public int count ( void )
public array exchangeArray ( mixed $input )
public array getArrayCopy ( void )
public int getFlags ( void )
public ArrayIterator getIterator ( void )
public string getIteratorClass ( void )
public void ksort ( void )
public void natcasesort ( void )
public void natsort ( void )
public bool offsetExists ( mixed $index )
public mixed offsetGet ( mixed $index )
public void offsetSet ( mixed $index , mixed $newval )
public void offsetUnset ( mixed $index )
public void serialize ( void )
public void setFlags ( int $flags )
public void setIteratorClass ( string $iterator_class )
public void uasort ( callable $cmp_function )
public void uksort ( callable $cmp_function )
public void unserialize ( string $serialized )
}

预定义常量

ArrayObject Flags

ArrayObject::STD_PROP_LIST

Properties of the object have their normal functionality when accessed as list (var_dump, foreach, etc.).

ArrayObject::ARRAY_AS_PROPS

Entries can be accessed as properties (read and write).

更新日志

版本 说明
5.3.0 Implements Serializable.

Table of Contents


各种类及接口
在线手册:中文  英文

用户评论:

mehea (2012-11-06 22:28:33)

Long story short b/c arrays by default are passed by value, if you pass an array to a function, the function works on a copy of the array while the original array remains unaltered by the function.

You may cause a change to the array to be reflected in the original array by having the function return the altered array and assign it to the variable for the original array, as follows:

<?php
function my_array_modify($data) { 
    
$data['b'] = 2;
    return 
$data;     


$regularArray = array(); 

$regularArray['a'] = 1
 
$regularArray my_array_modify($regularArray); 

var_dump($regularArray['b']); // 2 
?>

Or, you may explicitly pass the array by reference in which case there is no need for the function to return the array since the change will have effected the original array, as follows:

<?php
function my_arrayref_modify(&$data) { 
    
$data['bb'] = 22;

my_arrayref_modify($regularArray);
var_dump($regularArray['bb']); // 22
?>

tommash _at_ wp _dot_ pl (2012-05-17 17:51:19)

Please note, that there's another potential "gotcha" in using ArrayObject like regular array. Objects are reference types and arrays are value types. Consider following piece of code:
<?php

class MyArrayObject extends ArrayObject
{
    protected 
$data = array();
    public function 
offsetGet($name) {
        return 
$this->data[$name];
    }
    public function 
offsetSet($name$value) {
        
$this->data[$name] = $value;
    }
    public function 
offsetExists($name) {
        return isset(
$this->data[$name]);
    }
    public function 
offsetUnset($name) {
        unset(
$this->data[$name]);
    }
}

function 
my_array_modify($data) { //note - not reference &$data
    
$data['b'] = 2;
}

$regularArray = array();
$arrayObject = new MyArrayObject();

$regularArray['a'] = 1;
$arrayObject['a'] = 1;
my_array_modify($regularArray);
my_array_modify($arrayObject);
var_dump($regularArray['b']); // PHP Notice: Undefined index: b
var_dump($arrayObject['b']);  // int(2)
?>

php5 dot man at lightning dot hu (2012-01-05 08:24:37)

As you know ArrayObject is not an array so you can't use the built in array functions. Here's a trick around that:

Extend the ArrayObject class with your own and implement this magic method:

<?php
    
public function __call($func$argv)
    {
        if (!
is_callable($func) || substr($func06) !== 'array_')
        {
            throw new 
BadMethodCallException(__CLASS__.'->'.$func);
        }
        return 
call_user_func_array($funcarray_merge(array($this->getArrayCopy()), $argv));
    }
?>

Now you can do this with any array_* function:
<?php
$yourObject
->array_keys();
?>
- Don't forget to ommit the first parameter - it's automatic!

Note: You might want to write your own functions if you're working with large sets of data.

MarkAndrewSlade at gmail dot com (2011-08-21 18:05:28)

I found the description of STD_PROP_LIST a bit vague, so I put together a simple demonstration to show its behavior:

<?php                                                     
                                                          
$a 
= new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
    
$a['arr'] = 'array data';                             
    
$a->prop 'prop data';                               
$b = new ArrayObject();                                   
    
$b['arr'] = 'array data';                             
    
$b->prop 'prop data';                               
                                                          
// ArrayObject Object                                     
// (                                                      
//      [prop] => prop data                               
// )                                                      
print_r($a);                                              
                                                          
// ArrayObject Object                                     
// (                                                      
//      [arr] => array data                               
// )                                                      
print_r($b);                                              
                                                          
?>

marijn at sensimedia dot nl (2011-07-21 12:34:53)

A gotcha that is indeed mentioned in the manual, but isn't readily obvious and just cost me half an hour:
Objects implementing the Serializable interface do NOT get __sleep and __wakeup called; instead, they use serialize and unserialize methods, respectively (why, I don't know, but whatever - I'm sure there's a reason).
Hence, I was trying to serialize a database resultset in an object extending ArrayObject, and needed to fix some stuff regarding database resources on serialize. Took me a while to figure out __sleep wasn't getting called because ArrayObjects implements Serialize...
Presumably the ArrayObject internally implements the serialize/unserialize methods (in a trivial manner), hence the error wasn't apparent immediately (i.e., no fatal error was thrown) and I'd been trying to track why my objects didn't get serialized (they were of course) instead of renaming and fixing the methods.

sfinktah at php dot spamtrak dot org (2011-04-17 00:27:35)

If you plan to derive your own class from ArrayObject, and  wish to maintain complete ArrayObject functionality (such as being able to cast to an array), it is necessary to use ArrayObject's own private property "storage".

Since that is impossible to do directly, you must use ArrayObject's offset{Set,Get,Exists,Unset} methods to manipulate it indirectly.

As a side benefit, this means you inherit all the iteration and other functions in complete working order.

This may sound obvious to someone who has never implemented their own ArrayObject class...  but it is far from so.

<?php

class MyArrayObject extends ArrayObject {
        static 
$debugLevel 2;

        static public function 
sdprintf() {
                if (static::
$debugLevel 1) {
                        
call_user_func_array("printf"func_get_args());
                }
        }

        public function 
offsetGet($name) { 
                
self::sdprintf("%s(%s)\n"__FUNCTION__implode(","func_get_args()));
                return 
call_user_func_array(array(parent__FUNCTION__), func_get_args());
        }
        public function 
offsetSet($name$value) { 
                
self::sdprintf("%s(%s)\n"__FUNCTION__implode(","func_get_args()));
                return 
call_user_func_array(array(parent__FUNCTION__), func_get_args());
        }
        public function 
offsetExists($name) { 
                
self::sdprintf("%s(%s)\n"__FUNCTION__implode(","func_get_args()));
                return 
call_user_func_array(array(parent__FUNCTION__), func_get_args());
        }
        public function 
offsetUnset($name) { 
                
self::sdprintf("%s(%s)\n"__FUNCTION__implode(","func_get_args()));
                return 
call_user_func_array(array(parent__FUNCTION__), func_get_args());
        }
}

$mao = new MyArrayObject();
$mao["name"] = "bob";
$mao["friend"] = "jane";
print_r((array)$mao);

/* Output:
 
offsetSet(name,bob)
offsetSet(friend,jane)
Array
(
    [name] => bob
    [friend] => jane
)       */
?>

If you wish to use the "Array as Properties" flag, you simply need to include this in your constructor: 

<?php parent::setFlags(parent::ARRAY_AS_PROPS); ?>

This will allow you to do things such as the below example, without overriding __get or __set .

<?php
$mao
->name "Phil";
echo 
$mao["name"];   /* Outputs "Phil" */
?>

rob at tdd dot org dot uk (2011-03-03 04:38:16)

I don't believe the same performance is true since PHP 5.3. Using the same fill, read_key and foreach approach on both native arrays and ArrayObjects with 10000 keys I get the following

PHP 5.2

array() fill         0.013101
array() read         0.008685
array() foreach      0.004319
ArrayObject fill     0.014136
ArrayObject read     0.010003
ArrayObject foreach  3.454612

PHP 5.3

array() fill         0.010395
array() read         0.005933
array() foreach      0.001903
ArrayObject fill     0.010598
ArrayObject read     0.006387
ArrayObject foreach  0.003451

This was the code I used for both, an array or ArrayObject is passed into each of the functions. Again PEAR::Benchmark was used to get the results.

<?php
require_once 'Benchmark/Timer.php';

define('KEYS'10000);

function 
fill(&$arr) {
    for (
$i 1$i <= KEYS$i++) {
        
$arr['key-' $i] = $i;
    }
}

function 
read_key(&$arr) {
    for (
$i 1$i <= KEYS$i++) {
        
$v $arr['key-' $i];
    }
}

function 
fe(&$arr) {
    foreach (
$arr as $key => $value) {
        
$v $value;
    }
}
?>

danbettles at yahoo dot co dot uk (2009-08-23 08:56:41)

To implement array-style appending (e.g. "$object[] = 'foo';") in your own class implementing the ArrayAccess _interface_, all you need do is check if the key passed to your implementation of offsetSet() is NULL.  Something like the following.

<?php

class MyArrayObject implements ArrayAccess {

    
/**
     * @var array
     */
    
private $aValue;

    
// ...

    /**
     * @see ArrayAccess::offsetSet()
     */
    
public function offsetSet ($p_key$p_value) {
        if (
is_null($p_key)) {
            
$this->aValue[] = $p_value;
        }
        else {
            
$this->aValue[$p_key] = $p_value;
        }
    }

    
// ...
}
?>

skrebbel at gmail dot com (2009-04-18 03:25:50)

According to my benchmarks, doing foreach() on an ArrayObject is significantly slower than doing so on a vanilla array(). However, inserting keys and retrieving them is almost the same speed.
So, if performance is important, consider not using ArrayObject or descendant classes when you're iterating over its values a lot.
These are my timing results, using PEAR::Benchmark:
ArrayObject fill 0.01441502571106
ArrayObject read_key 0.018320083618164
ArrayObject read_foreach 2.1559031009674
array() fill 0.012364864349365
array() read_key 0.013092041015625
array() read_foreach 0.011217832565308
In all cases, 'fill()' inserts 10000 numbers at string keys, 'read_key()' reads all of those values by referencing the keys, and 'read_foreach()' does the same by walking through the array(object) with foreach().
As you can see, filling or reading from an ArrayObject by key is only 10% to 15% slower, but doing a foreach() is 200 times as costly. I am not sure what the cause of this may be.

nonproffessional at clockworkgeek dot com (2009-03-09 11:04:11)

To get a primitive array type back from an ArrayObject you can use the member function exchangeArray() or more simply just cast it:

<?php
$object 
= new ArrayObject();
$object[] = "Hello World!";
// $object is now an ArrayObject with one value appended.

$array = (array) $object;
// $array is now a simple array with the single value "Hello World!".
?>

deminy at deminy dot net (2009-01-02 13:31:50)

Generally variable $this can't be used as an array within an object context. For example, following code piece would cause a fatal error:

<?php
class TestThis {
    public function 
__set($name$val) {
        
$this[$name] = $val;
    }

    public function 
__get($name) {
        return 
$this[$name];
    }
}

$obj = new TestThis();
$obj->'aaa';
echo 
$obj->"\n";
?>

But things are different when $this is used in an ArrayObject object. e.g., following code piece are valid:

<?php
class TestArrayObject extends ArrayObject {    
    public function 
__set($name$val) {
        
$this[$name] = $val;
    }

    public function 
__get($name) {
        return 
$this[$name];
    }
}

$obj = new TestArrayObject();
$obj->'aaa';
echo 
$obj->"\n";
?>

Venelin Vulkov (2008-11-05 01:09:09)

Simple example of usage : 

<?php

$array 
= array('Buck','Jerry','Tomas');

$arrayObject = new ArrayObject($array);
// Add new element
$arrayObject->append('Tweety');

// We are getting the iterator of the object
$iterator $arrayObject->getIterator();

// Simple while loop
while ($iterator->valid()) {
    echo 
$iterator->current() . "\n";
    
$iterator->next();
}

/* Outputs */
Buck
Jerry
Tomas
Tweety

?>

Note that not all the public methods of this class are documented here .
( Which includes a lot sorting methods ) .

Regards

tony dot fraser at gmail dot com (2008-10-09 08:30:22)

The code above will pretty much work as it is, though I have since added in some features.

Further notes: 
1. Be very aware of this bug until 5.3 becomes stable.
http://bugs.php.net/bug.php?id=41528

Basically, we wrote this collection to be a java-like object cache stored in session (instead of  hitting the soap server or a DB all the time) to load objects in $_SESSION['cache']  But, this particular bug will prevent the object from working in the second page. The variable is there, but there's nothing in it.

The easy work around is to upgrade PHP to 5.3, and it works like a charm, but at the moment 5.3 happens to be in alpha mode.

2. You cannot have a PDO object in as a member variable of a class stored in the ArrayObject if you want it to pass through sessions. You can store it if it's not going into a session, but if you want to cache like we're tying to do, make SURE you $_dbHandle="" wherever you need to.

3. My earlier post was just proof of concept. We have it working now so  UserCollection extends GenericCollectionAbstract.  and GenericCollectionAbstract implements the GenericCollectionInterface. GenericCollectionObject is the object stored in the GenericCollection's $arrayObject data, and is basically just a two property object that holds the ID of the object, and the object itself.

When all is said and done this works like a charm.

page 1 ->
$_SESSION['u'] = new UserCollection();
$_SESSION['u']->getObject(1, $dbWW);
page-2->
echo $_SESSION['u']->getObject(2, $dbWW)->getProperty('email');

And of course, getProperty() is from my user class.

my UserCollecton->getObject overwrite method is as follows:

<?php
public function getObject($_id$_dbHandle){
        
//error_log('trying to get object');
        
if (parent::objectExists($_id)){
            return 
parent::getObject($_id$_dbHandle);
            
error_log('returning object');
        }
        else{
            
//error_log('collecting new user');
            
$_tempUser = new User();
            if (
$_tempUser->populateByID($_id$_dbHandle)){
                
parent::addObject($_id$_tempUser);
                if (
parent::objectExists($_id)) {
                    
$_return parent::getObject($_id);
                }
                else 
$_return "";
            }
            else {
                
$_return "";        
            }
            
$this->dbHandle=""//This has to be done, otherwise it's stored in the object as a private var
                                //and it will break the object if it resides in a session.
            
return $_return;
        }
?>

Shoot me an email if you have any questions.
enjoy.!

tony at tonyandcarol dot com (2008-10-07 19:49:22)

My need was to create a java-like collection where I could store objects by their DB primary keys while having the standard stack capabilities of adding, retrieving, and removing objects from collection. ArrayObject didn't quite do what I need it to do so I extended it a little.

<?php
class GenericCollection extends ArrayObject{
    private 
$data;
    function 
__construct(){
        
$this->data = new ArrayObject();
    }
    
    function 
addObject($_id$_object){
        
$_thisItem = new CollectionObject($_id$_object);
        
$this->data->offSetSet($_id$_thisItem);
    }
    function 
deleteObject($_id){
        
$this->data->offsetUnset($_id);
    }
    function 
getObject($_id){
        
$_thisObject $this->data->offSetGet($_id);
        return 
$_thisObject->getObject();
    }
    function 
printCollection() {
        
print_r($this->data);
    }
}

class 
CollectionObject {
    private 
$id;
    private 
$object;
    
    function 
__construct($_id$_object){
        
$this->id $_id;
        
$this->object $_object;
    }
    function 
getObject(){
        return 
$this->object;
    }
    function 
printObject() {
        
print_r($this);
    }
}
?>

Call it like so:

<?php
$u1 
= new User/Data/Object (); //whatever, just an object.

$myCollection = new GenericCollection();
$myCollection->addObject(1$u1);                    
print_r($myCollection->getObject(1));
?>

Now you have a simple and functional collection framework. Add methods in for specific types of sorting, we just didn't need anything other than primary key access. And you can add introspection into the collection object if you need to track what kind of an object it is.

tony@tonyandcarol.com

dave at csixty4 dot com (2008-09-05 08:28:29)

If you want to use array functions on an ArrayObject, why not use iterator_to_array() to get a standard PHP array? Do your operations on that array, then instantiate a new ArrayObject, passing it the array.
This might be a little slow on large ArrayObjects, but you'd have access to all of the array functions.

Anonymous (2008-08-09 18:17:58)

Too bad the Array functions [1] are not available on this object… otherwise I would be using it all the time.
[1] http://nl.php.net/manual/en/ref.array.php

易百教程