Skip to content

Instantly share code, notes, and snippets.

@anver
Last active July 29, 2018 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anver/0900cdb453eab6aeb1de70a52c8fe44f to your computer and use it in GitHub Desktop.
Save anver/0900cdb453eab6aeb1de70a52c8fe44f to your computer and use it in GitHub Desktop.
$a = array();
// Values inside of ArrayObject instances will be changed correctly, values
// inside of plain arrays won't
$a[] = array(new ArrayObject( range( 100, 200, 100 ) ),
new ArrayObject( range( 200, 100, -100 ) ),
range( 100, 200, 100 ));
$a[] = new ArrayObject( range( 225, 75, -75 ) );
// The array has to be
// - converted to an ArrayObject or
// - returned via $it->getArrayCopy()
// in order for this field to get handled properly
$a[] = 199;
// These values won't be modified in any case
$a[] = range( 100, 200, 50 );
// Comment this line for testing
$a = new ArrayObject( $a );
$it = new RecursiveIteratorIterator( new RecursiveArrayIterator( $a ) );
foreach ( $it as $k => $v ) {
// getDepth() returns the current iterator nesting level
echo $it->getDepth() . ': ' . $it->current();
if ( $v < 200 ) {
echo "\ttrue";
// This line is equal to:
// $it->getSubIterator($it->getDepth())->offsetSet($k, 0);
$it->getInnerIterator()->offsetSet( $k, 0 );
}
echo ($it->current() == 0) ? "\tchanged" : '';
echo "\n";
}
// In this context, there's no real point in using getArrayCopy() as it only
// copies the topmost nesting level. It should be more obvious to work with $a
// itself
print_r( json_decode( json_encode( $a ), TRUE ) );
//print_r($it->getArrayCopy());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment