PHP Debug Array or JSON object easily

///PHP Debug Array or JSON object easily

PHP Debug Array or JSON object easily

As a engineer I work with Arrays of Data, day in and day out. Whether its building them out or pulling them in from somewhere. More often than I’d like to admit something gets lost in translation and I have to see whats going on. Now most people will just simply do a quick debug statement like:\r\n\r\n
\r\n   echo "
";\r\n   print_r($arrayData);\r\n   echo "< /pre >";\r\n
\r\n\r\nand with the above, call it the day. However as I said I have to do it more often then I'd like to admit sometimes. With that I also have to do it with JSON Objects as well not just standard Array's so for myself I came up with a nifty little function to help me do either/or quickly mimicking the above output. Only thing I Need to do with mine however is type out a function name and pass two arguments with it to get the same result and it works with either a standard array or a JSON string.\r\n\r\n$arrayType can be either PHP or JSON (lowercase)\r\n$theArray would be the actual array of data either JSON or an Array.\r\n\r\n\r\nNote in the function above and below the end </pre> tags I had to break apart with spacing of the less-than and greater-than symbols otherwise it broke the display here on the page.\r\n\r\n
\r\n	function debugArray($arrayType = NULL, $theArray = NULL)\r\n	{\r\n		$output="success";\r\n		$msg="";\r\n		if(($theArray == NULL)||(count($theArray) <= 0))\r\n		{\r\n			$output="error";$msg .= "No Array to Process.";			\r\n		}\r\n		if(($arrayType == NULL)||(($arrayType !== 'php')AND($arrayType !== 'json')))\r\n		{\r\n			$output="error";$msg .= "No Array type Specified.";			\r\n		}\r\n	\r\n		if($output == "error")\r\n		{\r\n			echo $msg;\r\n			return false;\r\n		}\r\n		else\r\n		{\r\n			if(strtolower($arrayType) == 'php')\r\n			{\r\n				echo '
';\r\n				print_r($theArray);\r\n				echo '< /pre >';\r\n				return false;\r\n			}\r\n			elseif(strtolower($arrayType) == 'json')\r\n			{\r\n				echo '
';\r\n				echo print_r(json_decode($theArray));\r\n				echo '< /pre >';\r\n				return false;\r\n			}\r\n			else\r\n			{\r\n				echo "No Array type Specified.";\r\n				return false;\r\n			}\r\n		}\r\n	}\r\n
By |2014-01-05T10:22:43+00:00March 8th, 2012|Code, PHP|0 Comments

About the Author:

Not much to know about me, I'm a 35+ year old coder, geek, gamer..

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: