DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Get HTML List Of Array Keys

04.13.2005
| 17807 views |
  • submit to reddit
        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

Snippets Manager replied on Mon, 2012/05/07 - 2:11pm

You could just use print("
");
print_r($_POST);
print("
"); or print("
");
var_dump($_POST);
print("
");
A good tip I've picked up is to use print* for debug statements and echo for other output.