Here's a function I needed to step through a node in Drupal. It crawls through a combination of simple variables, arrays and class objects. Then it either calls the desired function or calls itself to iterate deeper.
function php_iterate($term) {
$item = get_object_vars($term);
if (is_object($term)) {
$new_value = new StdClass();
foreach ($item as $obj_name => $obj_value) {
if ((is_object($obj_value)) || (is_array($obj_value))) {
$obj_value = php_iterate($obj_value);
}
else {
$obj_value = drupal_convert_to_utf8($obj_value,'iso-8859-1');
}
$new_value->$obj_name = $obj_value;
}
}
else {
if (is_array($term)) {
$new_value = array();
foreach ($term as $obj_name => $obj_value) {
if ((is_object($obj_value)) || (is_array($obj_value))) {
$obj_value = php_iterate($obj_value);
}
else {
$obj_value = drupal_convert_to_utf8($obj_value,'iso-8859-1');
}
$new_value[$obj_name] = $obj_value;
}
}
else {
$new_value = drupal_convert_to_utf8($term,'iso-8859-1');
}
}
return $new_value;
}
Comments