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

About this user

Carsten Witt http://dailystar.de

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Get HTML list of Array Keys

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:
   1  
   2  /**
   3   * Creates recursively a nested HTML UL of array keys.
   4   * @param array $array Array
   5   * @return string Nested UL string
   6   */
   7  function Get_Array_Keys_UL($array=array()) {
   8   $recursion=__FUNCTION__;
   9   if (empty($array)) return '';
  10   $out='<ul>'."\n";
  11   foreach ($array as $key => $elem)
  12     $out .= '<li>'.$key.$recursion($elem).'</li>'."\n";
  13   $out .= '</ul>'."\n";  
  14   return $out;
  15  }


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

Get (recursive) directory listing in array #2

XoloX's cute function directoryToArray($directory, $recursive) will become "real recursive" by adding the recursion's directory content to array $array_items:
   1  
   2  if($recursive) { 
   3    #directoryToArray($directory. "/" . $file, true); 
   4    $array_items=array_merge($array_items,directoryToArray($directory. "/" . $file, $recursive));
   5  } 

String_Begins_With

No more letter counting... :-)
edit: see kirk's faster solution "String_Begins_With2"

   1  
   2  function String_Begins_With($needle, $subject) {
   3   return (substr($subject, 0, strlen($needle))==$needle);
   4  }

Flexible Singleton

PHP function Singleton allows you holding exactly one object of a class in memory. If an object of a given class doesn't exist, Singleton will create and store a new one in static array singleton. It then (and otherwise) will return a reference to it.

Adding an optional ID allows holding more than one object a class. Assigned ID will distinguish array keys.

   1  
   2  /**
   3   * Singleton Repository
   4   * @param string $class PHP Class Name
   5   * @param string $id Optional Object ID
   6   * @return reference Reference to existing Object
   7   */
   8  function &Singleton($class, $id='') {
   9    static $singleton = array();
  10    if (!array_key_exists($class.$id, $singleton))
  11      $singleton[$class.$id] = &new $class();
  12    $reference = &$singleton[$class.$id];
  13    return $reference;
  14  }


Use like this:
[edit] I'm sorry there was a mistake in the first exmaple for three days or so. Fixed.
   1  
   2  # first call: create object
   3  $site_user=&Singleton('Student');
   4  $site_user->Drink_Beer(5);
   5  
   6  # second call: get a reference
   7  $current_user=&Singleton('Student');
   8  echo $current_user->Show_Beers_Counter();
   9  #will be 5
  10  
  11  #Two different objects
  12  $one=&Singleton('Some_Class','one');
  13  $two=&Singleton('Some_Class','two');


Works fine with PHP4, not tested on PHP5
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS