This should do what you want:
function print_rn($array)
{
$nest_level = count(debug_backtrace()) - 1; // minus one to ignore the call to *this* function
$lines = explode("\n", print_r($array, true));
foreach ($lines as $line) {
echo str_repeat(" ", $nest_level) . $line . "\n";
}
}
Explanation:
print_r
takes a second argument, which allows you to return the value instead of printing it out. Then, you can use the explode
function (which is PHP's string_split function) to split the returned string into an array, at every newline. Now you have an array of lines.
With an array of lines, it's simple to iterate over each one and print it using the appropriate amount of whitespace.