note 107298 modified in function.print-r by danbrown

From: Date: Thu, 10 May 2012 21:28:31 +0000
Subject: note 107298 modified in function.print-r by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187895@lists.php.net to get a copy of this message
print_r is used for debug purposes. Yet I had some classes where I just wanted the values coming out of the database, not all the other crap. thus i wrote the following function. If your class has an toArray function, that one will be called otherwise it will return the object as is. print_neat_classes_r is the function that should be called!

<?php
print_neat_classes_r($array, $return=false){
		return print_r(self::neat_class_print_r($array), $return);
	}
	
function do_print_r($array, $return=false){
		if(is_object($array) && method_exists($array, 'toArray')){
			return $array->toArray();
		}else if(is_array($array)){
			foreach($array as $key=>$obj){
				$array[$key] = self::do_print_r($obj, $return);
			}
			return $array;
		}else{
			return $array;
		}
	}
?> --was-- print_r is used for debug purposes. Yet I had some classes where I just wanted the values coming out of the database, not all the other crap. thus i wrote the following function. If your class has an toArray function, that one will be called otherwise it will return the object as is. print_neat_classes_r is the function that should be called! print_neat_classes_r($array, $return=false){ return print_r(self::neat_class_print_r($array), $return); } function do_print_r($array, $return=false){ if(is_object($array) && method_exists($array, 'toArray')){ return $array->toArray(); }else if(is_array($array)){ foreach($array as $key=>$obj){ $array[$key] = self::do_print_r($obj, $return); } return $array; }else{ return $array; } } http://php.net/manual/en/function.print-r.php

« previous php.notes (#187895) next »