note 107370 added to class.splobjectstorage
| From: | HayleyWatson | Date: | Sat, 04 Feb 2012 12:16:07 +0000 |
| Subject: | note 107370 added to class.splobjectstorage | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185849@lists.php.net to get a copy of this message | ||
As an iterator, SplObjectStorage traverses the _keys_ of the map (the $o1, $o2 of Example 2) rather
than the values. To retrieve each of those in turn requires an additional lookup:
<?php
// As a map from objects to data
$s = new SplObjectStorage();
$o1 = (object)array('a'=>1);
$o2 = (object)array('b'=>2);
$o3 = (object)array('c'=>3);
$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);
foreach($s as $i => $key)
{
echo "Entry $i:\n"; // You get a numeric index
var_dump($key, $s[$key]);
echo "\n";
}
?>
This makes sense when you use an SplObjectStorage as a set, since in that case all of the
"values" are null.
----
Server IP: 202.89.58.1
Probable Submitter: 125.237.151.48
----
Manual Page -- http://www.php.net/manual/en/class.splobjectstorage.php
Edit -- https://master.php.net/note/edit/107370
Del: integrated -- https://master.php.net/note/delete/107370/integrated
Del: useless -- https://master.php.net/note/delete/107370/useless
Del: bad code -- https://master.php.net/note/delete/107370/bad+code
Del: spam -- https://master.php.net/note/delete/107370/spam
Del: non-english -- https://master.php.net/note/delete/107370/non-english
Del: in docs -- https://master.php.net/note/delete/107370/in+docs
Del: other reasons-- https://master.php.net/note/delete/107370
Reject -- https://master.php.net/note/reject/107370
Search -- https://master.php.net/manage/user-notes.php