Alternative var_dump for PHP that allows limiting depth of nested arrays

json_encode takes a depth argument. Do this:

echo '<pre>' . json_encode($your_array, JSON_PRETTY_PRINT, $depth) . '</pre>';


Here is the function for this issue:

function slice_array_depth($array, $depth = 0) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if ($depth > 0) {
                $array[$key] = slice_array_depth($value, $depth - 1);
            } else {
                unset($array[$key]);
            }
        }
    }

    return $array;
}

Use this function to slice an array to depth you need, than simply var_dump() or print_r() the sliced array :)


I'm just going to plug this var_dump alternative:

https://github.com/raveren/kint

obviously, depth limit is supported and enabled by default. You can furthermore, overcome it on the fly by prefixing Kint::dump($var); with a + - +Kint::dump($var); will not truncate based on depth.