BBS水木清华站∶精华区

发信人: thinkin (强强), 信区: Linux        
标  题: Print Array  
发信站: BBS 水木清华站 (Thu Feb 17 11:05:36 2000) 
 
<?php 
/* FIRETAIL-print_array 
 * Copyright (C) 1999 Scott Parish 
 * 
 * This library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Library General Public 
 * License as published by the Free Software Foundation; either 
 * version 2 of the License, or (at your option) any later version. 
 * 
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * Library General Public License for more details. 
 * 
 * You should have received a copy of the GNU Library General Public 
 * License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA. 
 */ 
//This is are two little functions that I made to quickly debug my arrays 
//while making php scripts.  You can pass these things any variable (both 
//arrays and non arrays), and it will print out everything that is in it. 
//It will even print out embedded arrays, so if you have arrays in arrays 
//in arrays, it will print all that out in a consice enough format that you 
//will know exactly what is in that variable. 
// 
//You should get output with "key => val", which should look something like 
//the following: 
// 
// admin => 
//         user           => 1 
//         screwball      => 2 
//         admin          => 4 
//         . . . 
// is    => 
//         student worker => 1 
//         hacker         => 2 
//         nerd           => 4 
//         admin          => 8 
// . . . 
// 
//call it like the following (where $testVar is a variable of you choice) 
// 
//  print_array($testVar); 
// 
//if your array contains any html code, you may want to call it instead as: 
// 
//  print_array($testVar,"<xmp>","</xmp>"); 
// 
//Should be very easy to throw in your code and use...hope you find it useful 
//            Scott Parish <sRparish@bigfoot.com> 1998/02/24 
 
if(!$GLOBALS[ "PRINT_ARRAY"]) { 
 
  $GLOBALS[ "PRINT_ARRAY"]=true; 
  function print_array($a,$btag= "",$etag= "") { 
    if(is_array($a)) { 
      printf( "<table cellpadding=0 cellspacing=0>"); 
      while(list($one,$two)=each($a)) { 
        printf( "\n<tr valign=baseline><td>$btag$one$etag</td><td>". 
                "&nbsp;$btag=>$etag</td>". 
                "<td align=right>&nbsp;%s</td></tr>\n" 
               ,sprint_array($two,$btag,$etag)); 
      } 
      printf( "</table>"); 
    } 
    else { 
      printf( "%s%s%s",$btag,$a,$etag); 
    } 
  } 
 
 
 
 
  function sprint_array($a,$btag= "",$etag= "") { 
    if(is_array($a)) { 
      $out=sprintf( "<table cellpadding=0 cellspacing=0>"); 
      while(list($one,$two)=each($a)) { 
        $out .= sprintf( "\n<tr valign=baseline><td>$btag$one$etag</td><td>". 
                         "&nbsp;$btag=>$etag</td>". 
                         "<td align=right>&nbsp;%s</td></tr>\n" 
                        ,sprint_array($two,$btag,$etag)); 
      } 
      $out .=  "</table>"; 
      return $out; 
    } 
    else { 
      return sprintf( "%s%s%s",$btag,$a,$etag); 
    } 
  } 

?> 
-- 
 
人生到处知何似? 
    应似飞鸿踏雪泥。 
        泥上偶然留指爪, 
            鸿飞那复计东西! 
        
 
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191] 

BBS水木清华站∶精华区