轻轻走过

我似风中落叶
posts - 2, comments - 0, trackbacks - 0, articles - 0
  PHP博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

工具函数array_dump()

Posted on 2006-08-05 19:19 轻轻走过 阅读(758) 评论(0)  编辑 收藏 引用 网摘
类似于var_dump() 区别在于它可以把输出当成字符串返回,而不是直接打印,
 1 function  array_dump(  $var  ) { 
 2
 3      $resString   =   "" ;
 4      //  The gettype function returns the data type 
 5     // of its parameter. 
 6      switch  ( gettype ( $var )) { 
 7
 8          //  inter, double, and strings are simply 
 9         // displayed. 
10          case   ' integer ' :  
11          case   ' double ' :  
12          case   ' string ' :  
13          $resString   .=   " $var\n "
14          break
15
16          //  array datatypes need to specially 
17         // handled. 
18          case   ' array ' :  
19          $resString   .=   " Array\n(\n " ;
20          //  if the array has no entries, display 
21         // a message. 
22          if  ( !   Count ( $var )) { 
23              $resString   .=   " \tEmpty Array.\n "
24         } 
25          else  { 
26
27
28              //  use a do loop to iterate over the 
29             // entries in the array. 
30              do  { 
31                  $resString   .=   " \t[ " . key ( $var ) . " ] =>  " ;
32
33                  //  perform the magic of recursion using the 
34                 // VALUE of the current key/value pair. 
35                  $resString   .=  array_dump( $var [ key ( $var )]); 
36             }  while  ( next ( $var )); 
37
38              //  end the HTML table after all of the 
39             //array entries have been displayed. 
40         } 
41          $resString   .=   " )\n " ;
42          break
43
44          //  switch statements should always have a default 
45         // clause - just in case the data has a value 
46         // your program doesn't expect. 
47          default :  
48          $resString   .=   " unknown data type\n "
49          break
50     } 
51      return   $resString ;
52

只有注册用户登录后才能发表评论。
网站导航: