Maxi-Pedia Forum

Information Technologies and Systems (IT/IS) => Other, SEO: Feel free to build your links here => Topic started by: atari on October 22, 2008, 05:36:44 pm



Title: How can I display content of array in PHP?
Post by: atari on October 22, 2008, 05:36:44 pm
I have an array whatever() which is a multidimensional array containing many many things. I want to display its content for debugging purpose. Is there some easy way to display array content?


Title: Re: How can I display content of PHP array ?
Post by: mod on October 22, 2008, 05:42:18 pm
There is one function running around the Internet which someone very smart wrote. I do not know who wrote it, but he was a genius. This function is awesome.

Use the following function to display the content of any multidimensional array:

Code:
function LIST_CONTENTS($arrayname,$tab="    ",$indent=0)
{   // recursively displays contents of the array and sub-arrays:
    // Free for unrestricted use, except sale - do not resell.
    // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent);
    // $tab = string to use as a tab, $indent = number of tabs to indent result
    while(list($key, $value) = each($arrayname))
    {
           for($i=0; $i<$indent; $i++) $currenttab .= $tab;
        if (is_array($value))
        {
            $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>";
            $retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>";
        }
           else $retval .= "$currenttab$key => $value<BR>";
           $currenttab = NULL;
    }
    return $retval;
}

This function is very useful when displaying for example the content of your $GLOBALS which can have many dimensions.

Use it like

Code:
echo LIST_CONTENTS($whateverarrayname);

Enjoy!


Title: Re: How can I display content of array in PHP?
Post by: bugibar on October 30, 2008, 08:46:41 pm
This is awesome function, I love it!!