note 32099 deleted from function.print-r by alindeman

From: Date: Fri, 16 May 2003 20:44:24 +0000
Subject: note 32099 deleted from function.print-r by alindeman
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-48116@lists.php.net to get a copy of this message
Note Submitter: av _at_ apollis.com (André Varney) Hi! Here we go for yet another function of this kind. But this one can even handle special result set style arrays having the same set of keys in each row and display them as a neat table. Cheers, André //=<imbp/func: Convert any variable to a string>=== //~ string VAL2STR(mixed VAL [, string PREFIX]) //. //. This function converts any variable to a //. human-readable string. It can even print //. result sets from database queries neatly //. as a table. You can also easily adjust //. the indentation. //. //. Example: //. print val2str($anyVar); //. // 2003 Apollis AG, Munich //-<imbp/tbl: ChangeLog>--------------------------- //. <Author ><Date ><Action > //. A. Varney 2003/05/16 Created //================================================= function val2str($val, $prefix='') { $indent = " "; // parse value if (!isset($val)) { $res = "NULL"; } else if (is_array($val)) { $elements = count($val); $res = "array[$elements]: "; $keyAr = array_keys($val); // find max. key length $keylenAr = array(); $maxkeylen = 0; foreach($keyAr as $key) { $len = strlen($key); $keylenAr[] = $len; if ($len > $maxkeylen) $maxkeylen = $len; } if ($elements) { // n elements if (is_array($val[0]) && is_array($val[1]) && (array_keys($val[0]) == array_keys($val[1]))) { // this is a result set -> print as a table // find out max. column size $collenAr = array(); foreach($val as $key => $rowAr) { foreach($rowAr as $rowkey => $rowval) { if (strlen($rowval) >= $collenAr[$rowkey]) $collenAr[$rowkey] = strlen($rowval); if (strlen($rowkey) >= $collenAr[$rowkey]) $collenAr[$rowkey] = strlen($rowkey); } } // print out table header $reskeyAr = array_keys($val[0]); $i = 0; $hdr1 = ''; $hdr2 = ''; foreach($reskeyAr as $reskey) { $hdr1 .= "|".sprintf("%-{$collenAr[$reskey]}s", $reskey); $hdr2 .= ($i++) ? "+": "|"; $hdr2 .= str_pad('', $collenAr[$reskey], '='); } $res .= "\n$prefix$hdr1|\n$prefix$hdr2|\n"; // print out table data foreach($val as $key => $rowAr) { $res .= $prefix; $i = 0; foreach($rowAr as $rowkey => $rowval) { $res .= ($i++) ? "" : "|"; $res .= sprintf("%{$collenAr[$rowkey]}s", val2str($rowval))."|"; } $res .= "\n"; } } else { // key/value array $is_assoc = (key($val) !== 0); $res .= "\n"; foreach($val as $key => $value) { $res .= "$prefix$indent"; if ($is_assoc) $res .= str_pad($key, $maxkeylen, '.') .": "; $subres = val2str($value, "$prefix$indent"); $res .= $subres; if (substr($subres, -1) != "\n") $res.= "\n"; } } } } else if (is_object($val)) { $class = get_class($val); $objAr['properties'] = get_object_vars($val); $objAr['methods'] = get_class_methods($class); $res.= "object of class [$class]: "; $res.= val2str($objAr, "$prefix"); } else if (is_bool()) { $res.= ($val) ? "TRUE" : "FALSE"; } else { $res.= (string)$val; } return $res; }

« previous php.notes (#48116) next »