Return to Snippet

Revision: 42196
at March 1, 2011 04:31 by daveespionage


Initial Code
function formatArrayForDisplay($array,$mode='human'){
	switch($mode){
		case 'human':
			// outputs array as human-readable:
			echo "<pre>" . print_r($array,true) . "</pre>";
			break;
		case 'PHP':
			// outputs array as PHP-ready string:	
			echo "array(";
			$comma = '';
			foreach($array as $key=>$value){
				echo "$comma ";
				if(is_array($value)){
					echo "array(";
					$comma2 = '';
					foreach($value as $key2=>$value2){
						echo "$comma2 '$key2'=>'$value2'";
						$comma2 = ',';
					};
					echo ")";		
					unset($comma2,$key2,$value2);			
				}else{
					echo "'$value'";
				}
				$comma = ',';
			};
			echo ");";
			unset($comma,$key,$value);		
			break;
	}
}

Initial URL


Initial Description
# function *formatArrayForDisplay*    

## parameters:    


* **array** 
a 1- or 2-dimensional array for output
* **mode**
accepts **human** or **PHP**
    * **human** displays using `<pre>` tags and print_r
    * **PHP** displays PHP style formatting for creating the same array using a PHP script, useful for unit tests when source data is needed.

## returns:

* **void**, all output is directed through **echo**

Initial Title
Array display utility for both Human-readable and PHP code drop-in

Initial Tags
php, array

Initial Language
PHP