note 93764 added to function.print-r
| From: | jeremy dot ruten at gmail dot com | Date: | Sun, 27 Sep 2009 02:16:03 +0000 |
| Subject: | note 93764 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161105@lists.php.net to get a copy of this message | ||
Here's an array_depth() function that calculates the depth of an array using the indentation in
the output of print_r():
<?php
function array_depth($array) {
$max_indentation = 1;
$array_str = print_r($array, true);
$lines = explode("\n", $array_str);
foreach ($lines as $line) {
$indentation = (strlen($line) - strlen(ltrim($line))) / 4;
if ($indentation > $max_indentation) {
$max_indentation = $indentation;
}
}
return ceil(($max_indentation - 1) / 2) + 1;
}
?>
It's better than writing a recursive function to do the same thing, as print_r() handles the
problem of infinitely-deep arrays for you quite nicely.
----
Server IP: 72.55.140.6
Probable Submitter: 71.17.240.44
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/93764
Del: integrated -- https://master.php.net/note/delete/93764/integrated
Del: useless -- https://master.php.net/note/delete/93764/useless
Del: bad code -- https://master.php.net/note/delete/93764/bad+code
Del: spam -- https://master.php.net/note/delete/93764/spam
Del: non-english -- https://master.php.net/note/delete/93764/non-english
Del: in docs -- https://master.php.net/note/delete/93764/in+docs
Del: other reasons-- https://master.php.net/note/delete/93764
Reject -- https://master.php.net/note/reject/93764
Search -- https://master.php.net/manage/user-notes.php