Quelltext der Datei: http://www.michaelster.ch/jsTestFolder/array_map/readme.txt

Dateigrösse: 4.31 kb

[Anzeige ohne Zeilennummern]


  1 ArrayMap
  2 
  3 Helps user to map a routine to each element of an array 
  4 and can also map a subroutine to each sub-array's element
  5 without any recursive function or method.and the array is
  6 traversed only once.
  7 
  8 
  9 Of course PHP got the function array_map but also the 
 10 functions array_walk and array_walk_recursive
 11 
 12 but the approach is not the same:
 13 
 14 -first the ArrayMap class methods don't use callback
 15 they use instead routine defined by the user itself
 16 so any valid PHP code can be used according to the logic
 17 or the purpose you want to achieve
 18 
 19 -second difference we never use recursive function,we just simulate recursion
 20 so the only limit will be memory usage limit and this for only really huge and
 21 deep array but in some cases only.
 22 
 23 
 24 This,said, let us come with an example
 25 require 'ArrayMap.php';
 26 
 27 $myroutine=<<<'EOF'
 28 if(isset($u)) 
 29     $u++;
 30 else 
 31     $u=0;
 32 array_multisort(%placeholder%,SORT_NUMERIC,SORT_DESC); 
 33 
 34 
 35 /*make the key available outside just for an example.
 36 It is just the same as pass a reference as argument*/
 37 $GLOBALS['key'.$u]= %key%; 
 38 
 39 EOF;
 40 
 41 $mysubroutine=<<<'EOF'
 42 global $myrefence;
 43 $myrefence++;
 44 if(is_string(%placeholder%)) 
 45     %placeholder%=strtoUpper(%placeholder%);
 46 elseif (is_numeric(%placeholder%)){
 47     %placeholder%*=%placeholder%;
 48 }
 49 else {
 50 
 51 %placeholder%=is_bool(%placeholder%)?(%placeholder%===true?'TRUE':'FALSE'):%placeholder%;
 52     %placeholder%=is_object(%placeholder%)?(array)%placeholder%:%placeholder%;
 53     // this eval won't be necessary if i use a string format which allows concatenation
 54     if(is_array(%placeholder%)) eval($aroutine); 
 55     if(!isset($aroutine))
 56         %placeholder%=is_array(%placeholder%)?array_sum(%placeholder%):%placeholder%;
 57 
 58 }
 59 
 60 EOF;
 61 
 62 
 63 $array[0]=range(1,10);
 64 $array[1]=range(1,10);
 65 $array[2]=range(1,10);
 66 $array[3]=range(1,10);
 67 $array[4]=[[[[[['badaboom']]]]]];
 68 $array[]=(object)[1,2,3];
 69 $array[]=(bool)[1,2,3];
 70 $array[]=(bool)[];
 71 
 72 /*this method do one thing: apply the mysubroutine for each 
 73  element in the array and return a new array*/
 74  
 75 print_r(arrayMap::aroutine($mysubroutine,$array));
 76 
 77 /*will help me count the number of single values in the array including 
 78 in sub_arrays exactly as the function count() just to show an example of reference*/
 79 
 80 $myrefence=null; 
 81 
 82 
 83 /*
 84 this method do two things: apply the mysburoutine for each single element in the 
 85 array and the myroutine for each array encountered and return a new array
 86 Note that myroutine is always applied before mysubroutine.
 87 */
 88  
 89 print_r(arrayMap::anvroutine($myroutine,$mysubroutine,$array));
 90 
 91 var_dump($myrefence);
 92 
 93 foreach($GLOBALS as $key=>$value){
 94     if(strpos($key,'key')===0) 
 95         echo "key: $key =>$value
 96             <br>";
 97     
 98 }
 99 
100 
101 
102 
103 You can see what this code does by running the example file in the package.
104 
105 But the resume in a few words is: go through the array and for each encountered 
106 if the element is object replace it by an array and then if the element is
107 array make a numeric sorting by descending order then for each element of each array
108 
109 while the element is sub-array again sort it ,if the element is string upper its case
110 if the element is numeric replace it by its square value , if the element is boolean
111 replace it either by "TRUE" for true or "FALSE" for false.Then return a new array.
112 And while we do all this collect some information with references.
113 
114 
115 
116 the two previous methods called this way will return a new array 
117 but you can force the original array to be modified directly use reference
118 this way:
119 
120 
121 print_r(arrayMap::aroutine($mysubroutine,null,$array));
122 
123 
124 print_r(arrayMap::anvroutine($mysubroutine,$myroutine,null,$array));
125 
126 note that both routines can be null string  or only one ,no errors will occur
127 
128 You can note with the example above some subtlety :for example references usages.
129 Keep also in mind that you can use any string format.
130 Finally errors in your routines will be detected and noticed with a precision on the line in the routine
131 and will make the methods trigger either warning for notice and warning or exception for fatal error 
132 like parse error and others.
133 
134 Last but not least error you must use %placeholder% in your code which mean the current value of the current array
135 and %key% for the current key... as used in the example.