PHP: flat_var_export()
I needed to log a nested array to a file. To do this I needed to convert the array to a single line. After some searching I found improved_var_export() that converts a PHP array/object to a single line textual representation. I cleaned up some of the formatting and this is the result.
function flat_var_export($variable, $return = false) {
if ($variable instanceof stdClass) {
$result = '(object) ' . flat_var_export(get_object_vars($variable), true);
} elseif (is_array($variable)) {
$array = array();
foreach ($variable as $key => $value) {
$array[] = var_export($key, true) . ' => ' . flat_var_export($value, true);
}
$result = 'array(' . implode(', ', $array) . ')';
} else {
$result = var_export($variable, true);
}
if ($return) {
return $result;
} else {
print $result;
}
}