PHP 8.5.0 Alpha 2 available for testing

Voting

: min(six, zero)?
(Example: nine)

The Note You're Voting On

Jan Walther
14 years ago
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());
?>

<< Back to user notes page

To Top