Array Debugging

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Array Debugging

Post by JayBird »

use this for array debugging. This is not my code, found it somewhere else.

Read the comments for useage

Code: Select all

<?
    //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 pr
    //     int 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 vari
    //     able.
    //
    //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 $tes
    //     tVar 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>","&
    //     lt;/xmp>");
    // 
    //Should be very easy to throw in your c
    //     ode and use...hope you find it useful
    //Scott Parish <sRparish@bigfoot.com&
    //     gt; 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>".
    " $btag=>$etag</td>".
    "<td align=right> %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>".
    " $btag=>$etag</td>".
    "<td align=right> %s</td></tr>\n"
    ,sprint_array($two,$btag,$etag));
    }
    $out .= "</table>";
    return $out;
    }
    else {
    return sprintf("%s%s%s",$btag,$a,$etag);
    }
    }
    }
    ?>
Mark
Post Reply