9

I try to use var_dump on command line with phpsh in order to get debugging information about some variable. But the variable contains a very deeply nested data structure. Therefore, using the default var_dump outputs too much information.

I want to limit the depth level of var_dump output. I found that XDebug's var_dump implementation allows depth limiting as described here: http://www.giorgiosironi.com/2009/07/how-to-stop-getting-megabytes-of-text.html

Unfortunately, I couldn't make this work neither. I don't know yet the reason for this. I am looking for if there are any alternative var_dump implementations to try.

Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117
  • have you tried *print_r*? it keeps less info – Igor Popov Nov 15 '12 at 10:58
  • 1
    Did you make a change in appropriate php.ini file? There is php.ini for CLI configuration (ie. /etc/php5/cli/php.ini) and another one for Apache configuration (ie. /etc/php5/apache/php.ini). – ivankoni Nov 15 '12 at 11:14
  • The xDebug solution does work, and it works well. What did you try? It is the best answer to your question. – SDC Nov 15 '12 at 11:25
  • 1
    @IgorPopov Yes, I tried `print_r`. It prints the whole object graph as well. @ivankoni Yes, I installed xdebug and made the change in appropriate php.ini file. I use webfaction servers. I changed ~/webapps//php.ini file. I added the line `xdebug.var_display_max_depth=1` – Mert Nuhoglu Nov 15 '12 at 12:32
  • Also you can use json_encode. echo @json_encode($var); It will print only readable values of your variable – Igor Popov Nov 15 '12 at 12:48
  • I created a function in which you can limit the depth and lines, and it formats a variable much like print_r. https://github.com/Xethron/to-string its also available on composer as "xethron/l4-to-string": "dev-master" – Xethron Feb 02 '14 at 11:29

3 Answers3

9

json_encode takes a depth argument. Do this:

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

rosell.dk
  • 2,228
  • 25
  • 15
  • This appeared to be a nice solution, but it strips too much information like object names. Therefore it is not very useful, unfortunately. – Code4R7 Jul 19 '18 at 10:46
5

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 :)

Dan K.K.
  • 5,915
  • 2
  • 28
  • 34
  • 2
    I think what he is asking is that the array values are array itself and he wants to print them such that after a certain depth,next element is printed. `print_r` will print the array with all it's depth – Abhishek Bhatia Nov 15 '12 at 11:02
  • What happens when `$depth=0` and `$value` is an array? I think it remains as an array and is not being chopped off. You will need an `elseif` for that in which `$array[$key]='Array at max deth';` must be there. – Abhishek Bhatia Nov 15 '12 at 11:30
  • You should pass a multidimensional array and depth to which you want to slice it – Dan K.K. Nov 15 '12 at 11:33
  • just checked, your function does not make any changes to the array. that is because when `$depth` has reached `0` and `$value` turns out to be an array, you are not actually slicing it off, it just remains unchanged. – Abhishek Bhatia Nov 15 '12 at 12:06
  • It slices an array's depth, not a length. – Dan K.K. Nov 15 '12 at 12:09
  • edited your answer to what I am saying, take a look at it. Also try to run your function,you will understand what I am saying.(Your function actually makes no changes) – Abhishek Bhatia Nov 15 '12 at 12:10
  • 1
    we need to pass $depth-1 into the function not decrease $depth by 1 in the current scope should be `$array[$key] = slice_array_depth($value, $depth-1);` – Lucas Aug 21 '14 at 11:44
  • To make this work, change the name of the `$array` var on lines 5, 7, and 12 to something else (I used `$sliced_array`). At least, having done that it works the way I had been expecting it to. :p – Sandwich Nov 30 '16 at 13:03
1

Check this :

function print_array($array,$depth=1,$indentation=0){
    if (is_array($array)){
                    echo "Array(\n";
        foreach ($array as $key=>$value){
            if(is_array($value)){
                if($depth <= 0){
                    echo "max depth reached.";
                }
                else{
                    for($i=0;$i<$indentation;$i++){
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    echo $key."=Array(";
                    print_array($value,$depth-1,$indentation+1);
                    for($i=0;$i<$indentation;$i++){
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    echo ");";
                }
            }
            else{
                for($i=0;$i<$indentation;$i++){
                    echo "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                echo $key."=>".$value."\n";
            }
        }
                    echo ");\n";
    }
    else{
        echo "It is not an array\n";
    }
}
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
Abhishek Bhatia
  • 716
  • 9
  • 26
  • Thank you, this solves my problem. There are a few syntax errors in the above code. When they are corrected, the function works as required. – Mert Nuhoglu Nov 15 '12 at 12:24