/[cvs]/nfo/php/libs/org.netfrag.flib/utils/Array_Manip.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.flib/utils/Array_Manip.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Wed Dec 11 00:22:23 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +11 -3 lines
+  function getModified()

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed