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

Friday, September 21, 2007

PHP : Outputting A to Z

for ($i="A";$i<="Z";$i++)

will output A to Z , but it will also output AA, AB, AC until it finishes with ZZ.

To just output A to Z do the following

for ($i="A";$i!="AA";$i++)