BigToach.com

PHP Snippets

print_array

Information

(PHP Version >= 4.0.1)
Functions used in this snippet: str_pad, array_keys, is_array, gettype.

Description

void print_array( array array, void level )

Prints the values and types of an array similarly to print_r but includes the values types and the arrays are colored as well according to the array colors.

Note: You don't need to do anything with the level variable.

Snippet

<?php
function print_array($array$level=0){
    
$num $level === 4;
    
$colors = array(    'bracket'=>'#999999',
                        
'value'=>'#FF0000',
                        
'array'=>'#009900',
                        
'arrow'=>'#999999',
                        
'type'=>'#0000FF',
                        
'key'=>'#000000');
    echo 
'<span style="color: ' $colors['array'] . ';">';
    echo 
"Array\n" str_pad('', ($level 4) + ($level $num), " ") . "(\n";
    
$keys array_keys($array);
    foreach(
$keys as $key){
        echo 
str_pad('', ($level 4) + ($level $num), " ");
        echo 
'    <span style="color: ' $colors['bracket'] . ';">';
        echo 
'[<span style="color: ' $colors['key'] . ';">' $key '</span>]';
        echo 
'</span> <span style="color: ' $colors['arrow'] . ';">=></span> ';
        if (
is_array($array[$key])){
            
print_array($array[$key], $level 1);
        }else{
            echo 
' <span style="color: ' $colors['value'] . ';">' $array[$key] . '</span>';
            echo 
' <span style="color: ' $colors['type'] . ';">' gettype($array[$key]) . "</span>\n";
        }
    }
    echo 
str_pad('', ($level 4) + ($level $num), " ") . ")</span>\n";
}
?>

Example

print_array Can be used in the following way:

<?php
echo '<pre>'// format to show whitespace
$array = array(1,1,1,2);
print_array($array); // prints out array, each with int as type

$another_array = array('wee''haha', array(1,2,3), array(array(null), array(true)));
print_array($another_array);
// prints out that array above.
?>

Added on Aug 12th at 2 am by Scott - 0 Comments


0 Responses to print_array

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: