About
Code - Functional programming (FP) - Collection Operations in Php is based on Php - Array (Map)
Function
Map
Map Simple iteration
- trim all values of the array from the character “
$commandArgs = ['"Hallo"','"World"'];
$commandArgs = array_map(
'trim',
$commandArgs,
array_fill(0,sizeof($commandArgs),"\"")
);
output:
$commandArgs = ['Hallo','World'];
Map using external variable
$externalVariable = 2;
$array = [1,2,3];
$newArray = array_map(
function ($element) use ($externalVariable ) {
return $element + $externalVariable;
},
$array
);
- Result
[3,4,5]
Filter
Functional Programming / Collection - Filter: array_filter
Filtering on key (by default, this is value)
$filteredArray = array_filter(
$array,
function($a) { return $a === "value" ; },
ARRAY_FILTER_USE_KEY
);
Reduce
Array walk ?
array_walk($array, function(&$a, $b) { $a = "$b foes $a"; });