Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Get HTML list of Array Keys (See related posts)

Sometimes I find it useful having an overview about the keys an array has; PHP function Get_Array_Keys_UL will create an nested HTML Unordered List string of the keys of a given array. Makes best sense with literal keys, e.g. $_POST:
/**
 * Creates recursively a nested HTML UL of array keys.
 * @param array $array Array
 * @return string Nested UL string
 */
function Get_Array_Keys_UL($array=array()) {
 $recursion=__FUNCTION__;
 if (empty($array)) return '';
 $out='<ul>'."\n";
 foreach ($array as $key => $elem)
   $out .= '<li>'.$key.$recursion($elem).'</li>'."\n";
 $out .= '</ul>'."\n";  
 return $out;
}


Use like this:
$ul=Get_Array_Keys_UL($_POST);
echo $ul;

Comments on this post

joshduck posts on Sep 17, 2005 at 03:38
You could just use
print("<pre>");
print_r($_POST);
print("</pre>");

or

print("<pre>");
var_dump($_POST);
print("</pre>");


A good tip I've picked up is to use print* for debug statements and echo for other output.

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts