Welcome to Maxi-Pedia Forum. Maxi-Pedia discussion forum is a free community inviting you to express your ideas and discuss various topics with other contributors.

November 22, 2024, 10:57:02 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Most Recent Posts:
Pages: [1]
  Print  
Author
Topic: 

How can I display content of array in PHP?

 (Read 11955 times)
atari
Full Member
***
Posts: 121


« 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?
Logged
Maxi-Pedia Forum
« on: October 22, 2008, 05:36:44 pm »

 Logged
mod
Moderator
*****
Posts: 525


« Reply #1 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!
Logged
bugibar
Full Member
***
Posts: 107


« Reply #2 on: October 30, 2008, 08:46:41 pm »

This is awesome function, I love it!!
Logged
Maxi-Pedia Forum
   

 Logged
Pages: [1]
  Print  
 
Jump to:  

Page created in 0.076 seconds with 23 queries. (Pretty URLs adds 0.001s, 0q)