note 57663 modified in ref.spl by helly
| From: | helly@php.net | Date: | Tue, 01 Nov 2005 21:34:33 +0000 |
| Subject: | note 57663 modified in ref.spl by helly | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97944@lists.php.net to get a copy of this message | ||
Something to note that, at least to me, seems pretty important and is not entirely clear in the
documentation is the fact that the ArrayObject class supports get/set on uni-dimensional keys and
get ONLY on *passed* multi-dimensional keys/paths (see source below). If you, like me, need to
support array accesss overloading for multi-dimensional data, you will need to derive from
ArrayObject and overide the ArrayAccess interface methods to "walk" passed data and
convert embedded arrays to objects of some kind...
Reference Bug 34816 @ http://bugs.php.net/bug.php?id=34816.
Illustration of the issue:
$a = array(
"test" => array(
"one" => "dunno",
"two" => array(
"peekabo" => "do you see me?",
"anyone" => array("there")
)
)
);
$oArray = new ArrayObject($a);
var_dump($oArray);
$oArray["three"] = "No problems here.";
echo "\n\\test\\one == " . $oArray["test"]["one"] . "\n\n";
// NEITHER of the two below will work!
$oArray["test"]["one"] = "Yes I do!";
$oArray["test"]["yes"] = array(
"hello" => "Goodbye!"
);
var_dump($oArray);
---
Note from the extension author:
Actually there is RecursiveArrayObject and RecursiveArrayIterator to deal with recursive structures.
However this does not always solve all multidimensional issues as expected.
--was--
Something to note that, at least to me, seems pretty important and is not entirely clear in the
documentation is the fact that the ArrayObject class supports get/set on uni-dimensional keys and
get ONLY on *passed* multi-dimensional keys/paths (see source below). If you, like me, need to
support array accesss overloading for multi-dimensional data, you will need to derive from
ArrayObject and overide the ArrayAccess interface methods to "walk" passed data and
convert embedded arrays to objects of some kind...
Reference Bug 34816 @ http://bugs.php.net/bug.php?id=34816.
Illustration of the issue:
$a = array(
"test" => array(
"one" => "dunno",
"two" => array(
"peekabo" => "do you see me?",
"anyone" => array("there")
)
)
);
$oArray = new ArrayObject($a);
var_dump($oArray);
$oArray["three"] = "No problems here.";
echo "\n\\test\\one == " . $oArray["test"]["one"] . "\n\n";
// NEITHER of the two below will work!
$oArray["test"]["one"] = "Yes I do!";
$oArray["test"]["yes"] = array(
"hello" => "Goodbye!"
);
var_dump($oArray);
http://php.net/manual/en/ref.spl.php