note 72394 added to function.print-r

From: Date: Wed, 17 Jan 2007 14:11:19 +0000
Subject: note 72394 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-121833@lists.php.net to get a copy of this message
i've been using a similar idea like Sawey's for quite a long time, but was always disappointed that you don't see the name of the variable passed to the function (eg calling it many times, you don't know which value was provided by a certain variable at a certain time). so i combined sawey's idea with one found on php.net to solve that problem. for shellscripting the function only uses <pre>-tags when called in the web. <?php /* how print_r should be ;) */ function my_r(&$var, $scope=false, $label=false){ if (is_string($scope) && $label==false) $label=$scope; if (!is_array($scope)) $scope = &$GLOBALS; $origin = $var; $var = $checker = "very_weird_value#".rand()."#isnt_it"; $vname = false; foreach ($scope as $key => $value){ if ($value === $checker){ $vname = "\$".$key; } if (is_array($value) && $key!="GLOBALS"){ if ($pfad=aver($var,$value)){ $vname = "\$${key}[\"". implode("\"][\"",$pfad)."\"]"; } } if (is_object($value)){ if ($pfad=aver($var,get_object_vars($value))){ if (sizeof($pfad)<2) $vname = "\$${key}->".$pfad[0]; else{ $vname ="\$${key}->".$pfad[0]; $vname.="[\"".implode("\"][\"", array_slice($pfad,1))."\"]"; } } } if ($vname) break; } $var = $origin; if ($_SERVER["SERVER_NAME"] && !isset($_SERVER["TERM"])) echo "<pre>"; if ($vname){ echo $vname; if ($label) echo " #" . $label . "# "; else echo ": "; }else{ if ($label) echo "$label: "; } print_r($var); if ($_SERVER["SERVER_NAME"] && !isset($_SERVER["TERM"])) echo "</pre>"; } /* this function is needed to check in multidimensional arrays if the value of interest exists. if yes, the path is returned */ function aver($needle,$haystack) { // array value exists recursive foreach($haystack as $key=>$val) { if(is_array($val) && $needle != $val && $key != "GLOBALS") { if($foo=aver($needle,$val)) return array_merge(array($key),$foo); }elseif($val === $needle) return array($key); } return false; } ?> ---- Server IP: 192.35.244.50 Probable Submitter: 193.170.250.70 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/72394 Del: integrated -- https://master.php.net/note/delete/72394/integrated Del: useless -- https://master.php.net/note/delete/72394/useless Del: bad code -- https://master.php.net/note/delete/72394/bad+code Del: spam -- https://master.php.net/note/delete/72394/spam Del: non-english -- https://master.php.net/note/delete/72394/non-english Del: in docs -- https://master.php.net/note/delete/72394/in+docs Del: other reasons-- https://master.php.net/note/delete/72394 Reject -- https://master.php.net/note/reject/72394 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121833) next »