Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
array_walk_recursive working for objects.
<?php
class A
{
private $ab = 5;
public $ac = 6;
}
/**
* WARNING: This https://bugs.php.net/bug.php?id=45937 seems not yet solved
* So, with this approach you could be accessing/changing private/protected
* members of the object.
*/
function walking_objects(&$value) {
if (is_object($value)) {
array_walk_recursive($value, "walking_objects");
} else {
$value = 20;
}
};
// $var can be an array or object
$var = array(new A(), 1, "c");
array_walk_recursive($var, "walking_objects");
print_r($var);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment