Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
 * Holds data so it can be passed around to various functions.
 *
 * Supplying the $type, $key and $bits (keyed array) will store it. Calling it
 * with $type and $key will retrieve everything previously stored for the
 * specific entry. Calling it with $type and the $key prepended with "get:"
 * will return all the matching keys previously set with $bits. And finally,
 * just calling it with no parameters will return everything stored.
 */
function _bit_pusher($type = '', $key = NULL, $bits = array()) {
  static $cache_bits = array();
  $output = array();
  
  if (!empty($type) && isset($key) && !empty($bits)) {
    $cache_bits[$type][$key] = isset($cache_bits[$type][$key]) ? array_merge($cache_bits[$type][$key], $bits) : $bits;
  }
  elseif (!empty($type) && isset($key)) {
    $key_hold = $key;
    $key = str_replace('get:', '', $key_hold);
    if ($key == $key_hold) {
      $output = isset($cache_bits[$type][$key]) ? $cache_bits[$type][$key] : $output;
    }
    elseif (isset($cache_bits[$type])) {
      foreach ($cache_bits[$type] as $id => $bits) {
        if (isset($bits[$key])) {
          if (is_array($bits[$key])) {
            $output = array_merge($output, $bits[$key]);
          }
          else {
            $output[$id] = $bits[$key];
          }
        }
      }
    }
  }
  elseif (!empty($type)) {
    $output = isset($cache_bits[$type]) ? $cache_bits[$type] : $output;
  }
  else {
    $output = !empty($cache_bits) ? $cache_bits : $output;
  }
  
  return $output;
}