Thursday, June 19, 2008

PHP - created a nested HTML unordered list from an array

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='
    '."\n";
    foreach ($array as $key => $elem)
    $out .= '
  • '.$key.$recursion($elem).'
  • '."\n";
    $out .= '
'."\n";
return $out;
}




Found here