简体   繁体   中英

Printing the structure of an array without its contents?

I was wondering if there is a way to print just the structure of the array without the contents. I generally use print_r to examine the structure but because my array contains some binary data, I'd rather not use this.

<?php
    function print_no_contents($arr) {
        foreach ($arr as $k=>$v) {
            echo $k."=> ";
            if (is_array($v)) {
                echo "\n";
                print_no_contents($v);
            }
            else echo "[data]";
            echo "\n";
        }
    }
?>

*didn't test this, but should get you started.

here is array structure with data

        echo printArray($your_array);

        function printArray($a,$return=true) {
                   if(!$return)
                      echo "<pre style=\"font-size:12px;\">".print_r($a,true)."</pre>";
                   else
                       return "<pre style=\"font-size:12px;\">".print_r($a,true)."</pre>";
        }

couldnt you just do

foreach ($array as $structure=>$data){
  echo $structure."=><br />";
}

I like to use xdebug's var_dump() overload for all of my variable snooping. You can provide it with an ini setting to truncate the values that are dumped out, and it provides some sane limites to begin with (though I'm not sure what it typically does with binary data).

ini_set('xdebug.var_display_max_data', 0);
var_dump($your_variable);

You can download it from http://xdebug.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM