.

Arr Class Reference
[Lib]

Encapsulates some convient array functions. More...

List of all members.

Static Public Member Functions

static  clean (&$clean, &$source)
  Static.
static  extract_array_keys ($key)
  Turn a string like a[b][c] into an array [a, b, c].
static  force ($value, $allow_empty=true)
  Force element into an array.
static  force_keys_to_string ($arr)
  Forces numeric array keys to strings.
static  get_item ($arr, $key, $default=false)
  Return value for key, if available, else return default value.
static  get_item_recursive ($arr, $key, $default=false)
  Return value for key, if available, else return default value.
static  implode ($glue, $pieces, $assign= '=')
  Implode associate array.
static  implode_tail ($glue, $glue_tail, $pieces)
  Implodes the array using $glues, but appends the last element using $glue_tail.
static  remove (&$arr, $value)
  Remove all occurences of a given value from array.
static  remove_recursive (&$arr, $value)
  Remove all occurences of a given value from array recursivley.
static  set_item_recursive (&$arr, $key, $value)
  Set value for key.
static  unforce_keys_from_string ($arr)
  Reverts results of force_keys_to_string.
static  unset_item_recursive (&$arr, $key)
  Unset entry in array.

Detailed Description

Encapsulates some convient array functions.

Author:
Gerd Riesselmann

Definition at line 8 of file array.cls.php.


Member Function Documentation

static Arr::clean ( &$  clean,
&$  source  
) [static]

Static.

Reads given array into given clean array

The associative array returned contains only members with keys that are defined in $clean. The values are taken form $source if available else they are taken from $clean.

Values from $source processed with strip_tags

Parameters:
Array  The clean array
Array  The array to read from

Definition at line 148 of file array.cls.php.

00148                                                         {
00149                 foreach($source as $key => $value)      {
00150                         if (array_key_exists($key, $clean))
00151                                 $clean[$key] = strip_tags($value);
00152                 }       
00153         }
static Arr::extract_array_keys ( key  )  [static]

Turn a string like a[b][c] into an array [a, b, c].

Parameters:
string|array  $key
Returns:
array

Definition at line 125 of file array.cls.php.

00125                                                         {
00126                 $ret = array();
00127                 if (is_array($key)) {
00128                         $ret = $key;
00129                 }
00130                 else {
00131                         $regex = '|\[(.*?)\]|';
00132                         $ret = preg_split($regex, $key, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
00133                 }
00134                 return $ret;
00135         }
static Arr::force ( value,
allow_empty = true  
) [static]

Force element into an array.

If passed an array, it returns the array, else it will return array($value)

Parameters:
mixed  $value
bool  $allow_empty If set to false, empty values will convert to an empty array

Definition at line 227 of file array.cls.php.

00227                                                                   {
00228                 if (is_array($value)) {
00229                         return $value;
00230                 }
00231                 $ret = array();
00232                 if ($allow_empty || !empty($value)) {
00233                         $ret[] = $value;
00234                 }
00235                 return $ret;
00236         }
static Arr::force_keys_to_string ( arr  )  [static]

Forces numeric array keys to strings.

Parameters:
array  $arr
Returns:
array

Definition at line 244 of file array.cls.php.

00244                                                           {
00245                 $ret = array();
00246                 foreach($arr as $key => $value) {
00247                         if (is_array($value)) {
00248                                 $value = self::force_keys_to_string($value);
00249                         }
00250                         if (is_numeric($key)) {
00251                                 $key = '__string_casted_' . $key; 
00252                         }
00253                         $ret[$key] = $value;
00254                 }
00255                 return $ret;
00256         }
static Arr::get_item ( arr,
key,
default = false  
) [static]

Return value for key, if available, else return default value.

Definition at line 12 of file array.cls.php.

00012                                                                       {
00013                 if (isset($arr[$key])) {
00014                         return $arr[$key];
00015                 }
00016                 return $default;        
00017         }
static Arr::get_item_recursive ( arr,
key,
default = false  
) [static]

Return value for key, if available, else return default value.

Key may contain [] to indicate array. item[0] would be retrieved as $arr['item']['0']

Parameters:
array  $arr
string|array  $key
mixed  $default

Definition at line 28 of file array.cls.php.

00028                                                                                 {
00029                 $ret = $default;
00030                 $arr_keys = self::extract_array_keys($key);
00031                 
00032                 if (count($arr_keys) > 0) {
00033                         $last_key = array_pop($arr_keys);
00034                         foreach($arr_keys as $key) {
00035                                 $arr = self::get_item($arr, $key, array());
00036                         }
00037                         $ret = self::get_item($arr, $last_key, $default);
00038                 }
00039                 
00040                 return $ret;
00041         }
static Arr::implode ( glue,
pieces,
assign = '='  
) [static]

Implode associate array.

Parameters:
string  String to put between elements of array
array  The array to implode
string  String to put between array key and value. If empty, function will fall back to normal implode
Returns:
string Imploded array as string

Definition at line 164 of file array.cls.php.

00164                                                                       {
00165                 $ret = '';
00166                 if (empty($assign)) {
00167                         $ret = implode($glue, $pieces);
00168                 }
00169                 else {
00170                         $bIsFirst = true;
00171                         foreach($pieces as $key => $value) {
00172                                 if ($bIsFirst == false) {
00173                                         $ret .= $glue;
00174                                 }
00175                                 $bIsFirst = false;
00176                                 $ret .= $key . $assign . $value;                        
00177                         }       
00178                 }
00179                 return $ret;
00180         }
static Arr::implode_tail ( glue,
glue_tail,
pieces  
) [static]

Implodes the array using $glues, but appends the last element using $glue_tail.

Example:

 $arr = array('cats', 'dogs', 'squirrels');
 print 'It\'s raining ' . Arr::implode_tail(', ', ', and ', $arr);
 // Outputs: It's raining cats, dogs, and squirrels.
 $arr = array('cats', 'dogs');
 print 'It\'s raining ' . Arr::implode_tail(', ', ', and ', $arr);
 // Outputs: It's raining cats, and dogs.
 $arr = array('cats');
 print 'It\'s raining ' . Arr::implode_tail(', ', ', and ', $arr);
 // Outputs: It's raining cats.
Since:
0.5.1
Parameters:
string  $glue
string  $glue_tail
array  $pieces
Returns:
string

Definition at line 206 of file array.cls.php.

00206                                                                         {
00207                 $ret = '';
00208                 $last = array_pop($pieces);
00209                 $ret .= implode($glue, $pieces);
00210                 if (!is_null($last)) {
00211                         if ($ret !== '') {
00212                                 $ret .= $glue_tail;
00213                         }
00214                         $ret .= $last;
00215                 }
00216                 return $ret;
00217         }
static Arr::remove ( &$  arr,
value  
) [static]

Remove all occurences of a given value from array.

Parameters:
array  $arr The array to modify
mixed  $value The value to unset

Definition at line 282 of file array.cls.php.

00282                                                      {
00283                 foreach(array_keys($arr, $value) as $key) {
00284                         unset($arr[$key]);
00285                 }
00286         }
static Arr::remove_recursive ( &$  arr,
value  
) [static]

Remove all occurences of a given value from array recursivley.

Parameters:
array  $arr The array to modify
mixed  $value The value to unset

Definition at line 294 of file array.cls.php.

00294                                                                {
00295                 self::remove($arr, $value);
00296                 foreach($arr as $key => $item) {
00297                         if (is_array($item)) {
00298                                 self::remove_recursive($item, $value);
00299                                 $arr[$key] = $item;
00300                         }
00301                 }
00302         }
static Arr::set_item_recursive ( &$  arr,
key,
value  
) [static]

Set value for key.

Key may contain [] to indicate array. item[0] would be retrieved as $arr['item']['0']

Parameters:
array  $arr
string|array  $key
mixed  $value

Definition at line 52 of file array.cls.php.

00052                                                                        {
00053                 $ret = false;
00054                 
00055                 $arr_keys = self::extract_array_keys($key);
00056                 if (count($arr_keys) == 0) {
00057                         return false;
00058                 }
00059                 
00060                 $last_key = array_pop($arr_keys);               
00061                 $arr_work =& $arr;
00062                 
00063                 while (is_array($arr_work)) {
00064                         $cur_key = array_shift($arr_keys);
00065                         if (is_null($cur_key)) {
00066                                 break;
00067                         }
00068                         if (!isset($arr_work[$cur_key])) {
00069                                 $arr_work[$cur_key] = array();
00070                         }
00071                         $arr_work =& $arr_work[$cur_key];
00072                 }
00073                 if (is_array($arr_work)) {
00074                         $arr_work[$last_key] = $value;
00075                         $ret = true;
00076                 }
00077                 
00078                 return $ret;
00079         }
static Arr::unforce_keys_from_string ( arr  )  [static]

Reverts results of force_keys_to_string.

Parameters:
array  $arr
Returns:
array

Definition at line 264 of file array.cls.php.

00264                                                               {
00265                 $ret = array();
00266                 foreach($arr as $key => $value) {
00267                         if (is_array($value)) {
00268                                 $value = self::unforce_keys_from_string($value);
00269                         }
00270                         $key = str_replace('__string_casted_', '', $key); 
00271                         $ret[$key] = $value;
00272                 }
00273                 return $ret;
00274         }
static Arr::unset_item_recursive ( &$  arr,
key  
) [static]

Unset entry in array.

Key may contain [] to indicate array. item[0] would be retrieved as $arr['item']['0']

Parameters:
array  $arr
string|array  $key

Definition at line 89 of file array.cls.php.

00089                                                                  {
00090                 $ret = false;
00091                 
00092                 $arr_keys = self::extract_array_keys($key);
00093                 if (count($arr_keys) == 0) {
00094                         return false;
00095                 }
00096                 
00097                 $last_key = array_pop($arr_keys);               
00098                 $arr_work =& $arr;
00099                 while (is_array($arr_work)) {
00100                         $cur_key = array_shift($arr_keys);
00101                         if (is_null($cur_key)) {
00102                                 break;
00103                         }
00104                         if (!isset($arr_work[$cur_key])) {
00105                                 break;
00106                         }
00107                         else {
00108                                 $arr_work =& $arr_work[$cur_key];
00109                         }
00110                 }
00111                 if (is_array($arr_work)) {
00112                         unset($arr_work[$last_key]);
00113                         $ret = true;
00114                 }
00115                 
00116                 return $ret;
00117         }

The documentation for this class was generated from the following file: