营销--孟介民--系列吧 关注:87贴子:1,342
  • 2回复贴,共1

PHP 打印对象、数组

只看楼主收藏回复

<?php

     function show_php($var,$indent='&nbsp;&nbsp;',$niv='0') {
         $str='';

         if (is_array($var)) {
             $str .= "<b>[array][".count($var)."]</b><br />";
             foreach ($var as $k=>$v) {
                 for($i=0;$i<$niv;$i++) {
                     $str.= $indent;
                 }
                 $str.= "$indent<em>\"{$k}\"=></em>";
                 $str.= show_php($v, $indent, $niv+1);
             }
         } elseif (is_object($var)) {
             $str .= "<b>[objet]-class=[" . get_class($var) . "]-method=[";
             $arr = get_class_methods($var);
                 foreach ($arr as $method) {
                     $str .= "[function $method()]";
                 }
             $str .= "]-";
             $str .= "</b>";
             $str .= show_php(get_object_vars($var), $indent, $niv+1);
         } else {
             $str .= "<em>[" . gettype($var) . "]</em>=[{$var}]<br />";
         }

         return($str);
     }

$tab=array(
     "first"=>"firstValue",
     "second"=>array(
         "first"=>1,
         "second"=>2,
         "third"=>array(
             "first"=>"one",
             "second"=>"two"
         ),
     ),
);

     echo "tab=".show_php($tab);
     echo "<hr />";
     var_dump($tab);
?>


回复
1楼2010-05-20 11:09
    打印结果:
    tab=[array][2]
       "first"=>[string]=[firstValue]
       "second"=>[array][3]
         "first"=>[integer]=[1]
         "second"=>[integer]=[2]
         "third"=>[array][2]
           "first"=>[string]=[one]
           "second"=>[string]=[two]

    --------------------------------------------------------------------------------
    array(2) { ["first"]=> string(10) "firstValue" ["second"]=> array(3) { ["first"]=> int(1) ["second"]=> int(2) ["third"]=> array(2) { ["first"]=> string(3) "one" ["second"]=> string(3) "two" } } }


    回复
    2楼2010-05-20 11:15
      递归


      回复
      3楼2010-05-20 11:15