PHP Classes

File: readme.txt

Recommend this page to a friend!
  Classes of zinsou A.A.E.Moïse   Array Map   readme.txt   Download  
File: readme.txt
Role: Documentation
Content type: text/plain
Description: readme
Class: Array Map
Traverse arrays to call functions for each entry
Author: By
Last change:
Date: 5 years ago
Size: 4,311 bytes
 

Contents

Class file image Download
ArrayMap Helps user to map a routine to each element of an array and can also map a subroutine to each sub-array's element without any recursive function or method.and the array is traversed only once. Of course PHP got the function array_map but also the functions array_walk and array_walk_recursive but the approach is not the same: -first the ArrayMap class methods don't use callback they use instead routine defined by the user itself so any valid PHP code can be used according to the logic or the purpose you want to achieve -second difference we never use recursive function,we just simulate recursion so the only limit will be memory usage limit and this for only really huge and deep array but in some cases only. This,said, let us come with an example require 'ArrayMap.php'; $myroutine=<<<'EOF' if(isset($u)) $u++; else $u=0; array_multisort(%placeholder%,SORT_NUMERIC,SORT_DESC); /*make the key available outside just for an example. It is just the same as pass a reference as argument*/ $GLOBALS['key'.$u]= %key%; EOF; $mysubroutine=<<<'EOF' global $myrefence; $myrefence++; if(is_string(%placeholder%)) %placeholder%=strtoUpper(%placeholder%); elseif (is_numeric(%placeholder%)){ %placeholder%*=%placeholder%; } else { %placeholder%=is_bool(%placeholder%)?(%placeholder%===true?'TRUE':'FALSE'):%placeholder%; %placeholder%=is_object(%placeholder%)?(array)%placeholder%:%placeholder%; // this eval won't be necessary if i use a string format which allows concatenation if(is_array(%placeholder%)) eval($aroutine); if(!isset($aroutine)) %placeholder%=is_array(%placeholder%)?array_sum(%placeholder%):%placeholder%; } EOF; $array[0]=range(1,10); $array[1]=range(1,10); $array[2]=range(1,10); $array[3]=range(1,10); $array[4]=[[[[[['badaboom']]]]]]; $array[]=(object)[1,2,3]; $array[]=(bool)[1,2,3]; $array[]=(bool)[]; /*this method do one thing: apply the mysubroutine for each element in the array and return a new array*/ print_r(arrayMap::aroutine($mysubroutine,$array)); /*will help me count the number of single values in the array including in sub_arrays exactly as the function count() just to show an example of reference*/ $myrefence=null; /* this method do two things: apply the mysburoutine for each single element in the array and the myroutine for each array encountered and return a new array Note that myroutine is always applied before mysubroutine. */ print_r(arrayMap::anvroutine($myroutine,$mysubroutine,$array)); var_dump($myrefence); foreach($GLOBALS as $key=>$value){ if(strpos($key,'key')===0) echo "key: $key =>$value <br>"; } You can see what this code does by running the example file in the package. But the resume in a few words is: go through the array and for each encountered if the element is object replace it by an array and then if the element is array make a numeric sorting by descending order then for each element of each array while the element is sub-array again sort it ,if the element is string upper its case if the element is numeric replace it by its square value , if the element is boolean replace it either by "TRUE" for true or "FALSE" for false.Then return a new array. And while we do all this collect some information with references. the two previous methods called this way will return a new array but you can force the original array to be modified directly use reference this way: print_r(arrayMap::aroutine($mysubroutine,null,$array)); print_r(arrayMap::anvroutine($mysubroutine,$myroutine,null,$array)); note that both routines can be null string or only one ,no errors will occur You can note with the example above some subtlety :for example references usages. Keep also in mind that you can use any string format. Finally errors in your routines will be detected and noticed with a precision on the line in the routine and will make the methods trigger either warning for notice and warning or exception for fatal error like parse error and others. Last but not least error you must use %placeholder% in your code which mean the current value of the current array and %key% for the current key... as used in the example.