PHP - Functional Programming

Card Puncher Data Processing

About

Code - Functional programming (FP) - Collection Operations in Php is based on Php - Array (Map)

Function

Map

Functional Programming - Map: array_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_reduce

Array walk ?

array_walk

array_walk($array, function(&$a, $b) { $a = "$b foes $a"; });





Discover More
Card Puncher Data Processing
Php - Array (Map)

In Php, an array is an(ordered|sorted) map (keys/value) Array can contain others arrays. The key can either be an integer or a string (in the same array - PHP does not distinguish between indexed...



Share this page:
Follow us:
Task Runner