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: 4 years ago
Size: 4,311 bytes


Class file image Download

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';


/*make the key available outside just for an example.
It is just the same as pass a reference as argument*/
$GLOBALS['key'.$u]= %key%; 


global $myrefence;
elseif (is_numeric(%placeholder%)){
else {

	// this eval won't be necessary if i use a string format which allows concatenation
	if(is_array(%placeholder%)) eval($aroutine); 




/*this method do one thing: apply the mysubroutine for each 
 element in the array and return a new 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*/


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.


foreach($GLOBALS as $key=>$value){
		echo "key: $key =>$value

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:



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. 
For more information send a message to info at phpclasses dot org.