PHP Classes

Array Map: Traverse arrays to call functions for each entry

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 169 This week: 1All time: 8,846 This week: 560Up
Version License PHP version Categories
array-map-routine 1.0.0The PHP License5PHP 5, Data types
Description 

Author

This class can traverse arrays to call functions for each entry.

It takes an array for each entry it calls a given function and then for each entry it calls another function to map the respective values.

Innovation Award
PHP Programming Innovation award nominee
August 2018
Number 4
PHP provides a function for recreating an array from another array by calling a function that will change the original values of the array entries.

This class provides a more advanced array mapping functionality by allowing to have two different functions that will be called on nested arrays with two depth levels.

This makes it possible to implement different types of array value mapping depending on the level of depth of the values of each entry.

Manuel Lemos
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6781 in Benin Benin
Week rank: 109 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('ArrayMap.php');



$aroutine=<<<'EOF'
if(isset($u)) $u++;
else $u=0;
array_multisort(%placeholder%,SORT_NUMERIC,SORT_DESC);
$GLOBALS['key'.$u]= %key%; //make the key available outside just for an example.It is just the same as pass a reference as argument
// %placeholder%=array_sum(%placeholder%);
EOF;

$vroutine=<<<'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%;
    if(is_array(%placeholder%)) eval($aroutine);
    if(!isset($aroutine))
        %placeholder%=is_array(%placeholder%)?array_sum(%placeholder%):%placeholder%;
   
   
}

EOF;

echo
'<pre>';
$array[0]=array(range(1,10),range(20,-20,2));
$array[1]=range(1,10);
$array[2]=range(1,10);
$array[3]=range(1,10);
$array[4]=[[['Bigbadaboom',[[['badaboom']]]],'Gigabigboom']];
$array[]=(object)[1,2,3];
$array[]=(bool)[1,2,3];
$array[]=(bool)[];


print_r(arrayMap::aroutine($vroutine,$array));

$myrefence=null;//will help me count the number of single values in the array including in sub_arrays exactly as the function count()
print_r(arrayMap::anvroutine($aroutine,$vroutine,$array));

var_dump($myrefence);
foreach(
$GLOBALS as $key=>$value){
    if(
strpos($key,'key')===0)
        echo
"key: $key =>$value
            <br>"
;
   
}

print_r(arrayMap::aroutine($vroutine,null,$array));

$myrefence=null;//will help me count the number of single values in the array including in sub_arrays exactly as the function count()

print_r(arrayMap::anvroutine($aroutine,$vroutine,null,$array));

var_dump($myrefence);




?>


Details

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.

  Files folder image Files  
File Role Description
Plain text file ArrayMap.php Class class source
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file testArrayMap.php Example example script

 Version Control Unique User Downloads Download Rankings  
 0%
Total:169
This week:1
All time:8,846
This week:560Up