note 96914 added to ref.array
| From: | oliverSPAMMENOT at e-geek dot com dot au | Date: | Tue, 23 Mar 2010 03:28:18 +0000 |
| Subject: | note 96914 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167301@lists.php.net to get a copy of this message | ||
Function to pretty print arrays and objects. Detects object recursion and allows setting a maximum
depth. Based on arraytostring and u_print_r from the print_r function notes. Should be called like
so:
<?php
egvaluetostring($value) //no max depth, or
egvaluetostring($value, $max_depth) //max depth set
?>
<?php
function egvaluetostring($value, $max_depth, $key = NULL, $depth = 0, $refChain = array()) {
if($depth > 0)
$tab = str_repeat("\t", $depth);
$text .= $tab . ($key !== NULL ? $key . " => " : "");
if (is_array($value) || is_object($value)) {
$recursion = FALSE;
if (is_object($value)) {
foreach ($refChain as $refVal) {
if ($refVal === $value) {
$recursion = TRUE;
break;
}
}
array_push($refChain, $value);
}
$text .= (is_array($value) ? "array" : "object") . " ( ";
if ($recursion) {
$text .= "*RECURSION* ";
}
elseif (isset($max_depth) && $depth >= $max_depth) {
$text .= "*MAX DEPTH REACHED* ";
}
else {
if (!empty($value)) {
$text .= "\n";
foreach ($value as $child_key => $child_value) {
$text .= egvaluetostring($child_value, $max_depth, (is_array($value) ? "[" :
"") . $child_key . (is_array($value) ? "]" : ""), $depth+1, $refChain)
. ",\n";
}
$text .= "\n" . $tab;
}
}
$text .= ")";
if (is_object($value)) {
array_pop($refChain);
}
}
else {
$text .= "$value";
}
return $text;
}
----
Server IP: 69.147.83.197
Probable Submitter: 121.44.108.220
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/96914
Del: integrated -- https://master.php.net/note/delete/96914/integrated
Del: useless -- https://master.php.net/note/delete/96914/useless
Del: bad code -- https://master.php.net/note/delete/96914/bad+code
Del: spam -- https://master.php.net/note/delete/96914/spam
Del: non-english -- https://master.php.net/note/delete/96914/non-english
Del: in docs -- https://master.php.net/note/delete/96914/in+docs
Del: other reasons-- https://master.php.net/note/delete/96914
Reject -- https://master.php.net/note/reject/96914
Search -- https://master.php.net/manage/user-notes.php