PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ArrayObject::count> <ArrayObject::append
Last updated: Sun, 25 Nov 2007

view this page in

ArrayObject::__construct

(PHP 5)

ArrayObject::__construct — Construct a new array object

Description

ArrayObject ArrayObject::__construct ( mixed $input )

This constructs a new array object. The input parameter accepts an array or another ArrayObject.

Example#1 ArrayObject::__construct() example

<?php
$array 
= array('1' => 'one',
               
'2' => 'two',
               
'3' => 'three');

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);
?>

Výstup horeuvedeného príkladu bude:

object(ArrayObject)#1 (3) {
  [1]=>
  string(3) "one"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
}



ArrayObject::count> <ArrayObject::append
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
ArrayObject::__construct
kjarli at gmail dot com
03-Jul-2008 03:11
This extend allows multidimensional arrays to be converted aswell. It also returns 'Array' when echoed (unlike ArrayObject which gives an error).
<?php
/**
 * @author iltar van der berg
 * @version 1.0.1
 */
class RecursiveArrayObject extends ArrayObject
{
   
/**
     * overwrites the ArrayObject constructor for
     * iteration through the "array". When the item
     * is an array, it creates another self() instead
     * of an array
     *
     * @param Array $array data array
     */
   
public function __construct(Array $array)
    {   
        foreach(
$array as $key => $value) {
            if(
is_array($value)){
               
$value = new self($value);
            }
           
$this->offsetSet($key, $value);
        }
    }
   
   
/**
     * returns Array when printed (like "echo array();")
     * instead of an error
     *
     * @return string
     */
   
public function __ToString()
    {
        return
'Array';
    }
}
?>
german dot rumm at gmail dot com
14-Mar-2008 11:37
BTW, if you need to change array later, use exchangeArray() method. Good to know when you are writing a class that extends ArrayObject()

AFAIK, exchangeArray() doesn't return anything.

<?php
    $a
= array('one', 'two', 'three');
   
$ao = new ArrayObject($a);

    foreach (
$ao as $element) {
        echo
$element . ' '; // one two three
   
}

   
$b = array('four', 'five', 'six');
   
$ao->exchangeArray($b); // returns null

   
foreach ($ao as $element) {
        echo
$element . ' '; // four five six
   
}
?>
agalkin at agalkin dot ru
21-Aug-2007 01:56
Note that the first argument to ArrayObject::__construct, the initial array, is passed by reference. Nevertheless, modification of the array doesn't modify the object, so it may cause unexpected behaviour.

<?php
$array
= array('foo' => 'initial');
$obj = new ArrayObject($array);

// array was passed by reference:
$obj['foo'] = 'modified';
var_dump($array); // foo => modified

// but it doesn't work backwards:
$array['foo'] = 'modified_again';
var_dump($obj); // foo => modified
var_dump($array); // foo => modified_again
?>

ArrayObject::count> <ArrayObject::append
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites