array_walk debug example

Shows how to use the array_walk function to debug and print an array in a human readable format.

function debug_val($val, $key='', $depth=0) {
 
    if (is_array($val)){
        // call this function again with the "sub-array":
        array_walk($val, 'debug_val', $depth+5);
    }
    else {
        // if we hit a string or bool, etc. then print it:
        print str_repeat(' ', $depth);          
        print '<span style="color: blue;">' . $key . '</span>: ';
        print var_export($val, true);
        print "<br/>\n";
    }
}
 
 
/*example-start*/
 
// setup the test array 
$array = array(
    'php', 
    'cool', 
    array('foo', 1,2,3, array('mixed' => 'bar')),
    'php' => 'array', 
    'yes' => true, 'no' => false
);
 
// debug the array
debug_val($array);
 
/*example-end*/
Snippet Details




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.