note 104682 modified in class.splobjectstorage by danbrown
| From: | danbrown@php.net | Date: | Thu, 30 Jun 2011 14:35:38 +0000 |
| Subject: | note 104682 modified in class.splobjectstorage by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181108@lists.php.net to get a copy of this message | ||
I rewrote some scripts and changed object storage with arrays to SplObjectStorage. At some point I
needed support of array_rand() but I did not find a function to return a random attached object of
an SplObjectStorage object.
So here is my solution for random access to SplObjectStorage:
<?php
$o1 = new StdClass;
$o2 = new StdClass;
$s = new SplObjectStorage;
$s->attach($o1);
$s->attach($o2);
$random = rand(0,$s->count()-1);
$s->rewind();
for($i=0;$i<$random;$i++) {
$s->next();
}
var_dump($s->current());
?>
--was--
I rewrote some scripts and changed object storage with arrays to SplObjectStorage. At some point I
needed support of array_rand() but I did not find a function to return a random attached object of
an SplObjectStorage object.
So here is my solution for random access to SplObjectStorage:
$o1 = new StdClass;
$o2 = new StdClass;
$s = new SplObjectStorage;
$s->attach($o1);
$s->attach($o2);
$random = rand(0,$s->count()-1);
$s->rewind();
for($i=0;$i<$random;$i++) {
$s->next();
}
var_dump($s->current());
http://php.net/manual/en/class.splobjectstorage.php