1 |
<? |
2 |
// ------------------------------------------------------------------------- |
3 |
// $Id$ |
4 |
// ------------------------------------------------------------------------- |
5 |
// $Log$ |
6 |
// ------------------------------------------------------------------------- |
7 |
|
8 |
|
9 |
// ------------------------------------------------------------------ |
10 |
// takes a string and searches values of haystack to remove this in place |
11 |
// returns "true" if operation succeeded |
12 |
function removeNeedle($needle, &$haystack) { |
13 |
$arrpos = array_search($needle, $haystack); |
14 |
if (is_numeric($arrpos)) { |
15 |
array_splice($haystack, $arrpos, 1); |
16 |
return true; |
17 |
} |
18 |
return false; |
19 |
} |
20 |
|
21 |
// ------------------------------------------------------------------ |
22 |
// removes single or multiple elements from haystack |
23 |
// takes a string or an array containing scalars |
24 |
// matching the to-be-removed values |
25 |
function removeNeedles($needle, &$haystack) { |
26 |
$res = true; |
27 |
if (is_array($needle)) { |
28 |
foreach ($needle as $val) { |
29 |
$res &= removeNeedle($val, $haystack); |
30 |
} |
31 |
} else { |
32 |
$res &= removeNeedle($needle, $haystack); |
33 |
} |
34 |
return $res; |
35 |
} |
36 |
|
37 |
|
38 |
function prefixElements_walker(&$item, $key, $prefix) { |
39 |
$item = $prefix . $item; |
40 |
} |
41 |
|
42 |
|
43 |
class Array_Manip { |
44 |
|
45 |
var $haystack; |
46 |
|
47 |
function Array_Manip(&$haystack, $args = array()) { |
48 |
$this->haystack = $haystack; |
49 |
} |
50 |
|
51 |
function remove($needle, $level) { |
52 |
return $this->_rm_needle_bylevel($needle, $level); |
53 |
} |
54 |
|
55 |
// ------------------------------------------------------------------ |
56 |
// removes single or multiple elements from haystack |
57 |
// takes an additional argument: the nesting level inside haystack to operate on |
58 |
function _rm_needle_bylevel($needle, $level) { |
59 |
global $cLevel, $tLevel, $array_walk_found; |
60 |
$res = true; |
61 |
|
62 |
$array_walk_found = false; |
63 |
$cLevel = 0; |
64 |
$tLevel = $level; |
65 |
|
66 |
if (!is_array($this->haystack)) { return; } |
67 |
|
68 |
array_walk($this->haystack, array($this, '_rm_needle_bylevel_walker'), $needle); |
69 |
return $array_walk_found; |
70 |
} |
71 |
|
72 |
// walker function for removing needles by level |
73 |
function _rm_needle_bylevel_walker(&$item, $key, $arg_needle) { |
74 |
global $cLevel, $tLevel, $array_walk_found; |
75 |
$cLevel++; |
76 |
if (is_array($item)) { |
77 |
if ($cLevel == $tLevel) { |
78 |
$array_walk_found |= removeNeedles($arg_needle, $item); |
79 |
//$array_walk_found |= $this->_rm_needle_multi($arg_needle); |
80 |
} else { |
81 |
array_walk($item, array($this, '_rm_needle_bylevel_walker'), $arg_needle); |
82 |
} |
83 |
} else { |
84 |
} |
85 |
$cLevel--; |
86 |
} |
87 |
|
88 |
|
89 |
function prefixElements($array, $prefix) { |
90 |
//if (!is_array($this->haystack)) { $this->haystack = array(); } |
91 |
//array_walk($this->haystack, array($this, 'prefixElements_walker'), $prefix); |
92 |
//array_walk($array, array($this, 'prefixElements_walker'), $prefix); |
93 |
array_walk($array, 'prefixElements_walker', $prefix); |
94 |
return $array; |
95 |
} |
96 |
|
97 |
|
98 |
function buildSymmetricIntegerHash($begin, $end) { |
99 |
$res = array(); |
100 |
if(!$begin || !is_int($begin) || !$end || !is_int($end)) { |
101 |
$res[0] = "Error: Function 'buildSymmetricIntegerHash' can only used with Integer as Variable Type!"; |
102 |
return $res; |
103 |
} |
104 |
$diff = $end - $begin; |
105 |
if($diff<=0) { |
106 |
$res[0] = "Error: Var 'end' in Function 'buildSymmetricIntegerHash' is smaller or eqal asVar 'begin' !"; |
107 |
return $res; |
108 |
} |
109 |
$this->haystack = array(); |
110 |
for ($i=$begin;$i<=$end;$i++) { |
111 |
$this->haystack[$i] = $i; |
112 |
} |
113 |
return $this->haystack; |
114 |
} |
115 |
|
116 |
|
117 |
} |
118 |
|
119 |
?> |