array_walk debug example

Shows how to use the array_walk function to debug and print an array in a human readable format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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($valtrue);
        print "<br/>\n";
    }
}
 
 
/*example-start*/
 
// setup the test array 
$array = array(
    'php'
    'cool'
    array('foo'1,2,3array('mixed' => 'bar')),
    'php' => 'array'
    'yes' => true'no' => false
);
 
// debug the array
debug_val($array);
 
/*example-end*/
X

Url: http://www.jonasjohn.de/snippets/php/array-walk-debug.htm

Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013