A Simple Example of php Functional Programming

  • 2021-12-19 06:02:48
  • OfStack

This paper describes php functional programming with examples. Share it for your reference, as follows:


//  Functional programming 
$users = array(
  array('id' => 1, 'name' => 'abc1', 'age' => 29, ' Gender ' => ' Male '),
  array('id' => 2, 'name' => 'abc2', 'age' => 21, ' Gender ' => ' Female '),
  array('id' => 3, 'name' => 'abc3', 'age' => 23, ' Gender ' => ' Male '),
  array('id' => 4, 'name' => 'abc4', 'age' => 25, ' Gender ' => ' Female '),
  array('id' => 5, 'name' => 'abc5', 'age' => 20, ' Gender ' => ' Female '),
  array('id' => 6, 'name' => 'abc6', 'age' => 24, ' Gender ' => ' Male '),
  array('id' => 7, 'name' => 'abc7', 'age' => 28, ' Gender ' => ' Female '),
  array('id' => 8, 'name' => 'abc8', 'age' => 27, ' Gender ' => ' Male '),
);
// Get users whose gender is female 
$arrayFilter = array_filter($users, function($item){
  return $item[' Gender '] == ' Female ' ;
});
//  Does not affect the original array and returns 1 New arrays 
$arrayMap = array_map(function($item){
  return array(
    'id' => $item['id'],
    'name' => $item['name'],
    'age' => $item['age'],
    'gender' => $item[' Gender '] == ' Male ' ? 'male' : 'female',
  );
}, $users);
//  Modify the original array, and change the age +10 Processing while adding indexes gender , return value  1  Or  0
array_walk($users, function(&$item, $index){
  $item['gender'] = $item[' Gender '] == ' Male ' ? 'male' : 'female';
  if ($index % 2 == 0) {
    $item['age'] += 10;
  }
});
//array_reduce(array $input , callable $function [,$initial = NULL ])  Iteratively simplifying an array to a single 1 Value of 
//  Find the oldest user , Returns maximum age user information 
$arrayReduce = array_reduce($users, function($init, $val){
  return $init['age'] > $val['age'] ? $init : $val;
}, array('age' => 0));
//  Calculate the average age 
$avgAge = array_reduce($users, function($init, $item){
  return $init + $item['age'];
}, 0) / count($users);
/*
 * array_reduce  Internal implementation of 
function array_reduce($data, $callback, $initial) {
  foreach ($data as $index => $val) {
    $initial = $callback($initial, $val);
  }
  return $initial;
}
*/
// Use array_map And array_mutisort To sort  
// Utilization array_map Gets the array to sort by ,( Anonymous function  create_function($args, return $val))
//$arrField = array_map(create_function('$item', 'return $item["age"];'), $users);  "Not Recommended" 
$arrField = array_map(function($item){
  return $item['age'];
}, $users);
// Utilization array_mutisort To sort the ages from big to small 
$arrSort = array_multisort($arrField, SORT_DESC, $users);


// Closure implementation counter 
function counts() {
  $a = 1;
  //  Closure , Reference variable $a
  return function() use(&$a) {
    return $a++;
  };
}
$countFunc = counts();
echo $countFunc(); // 1
echo $countFunc(); // 2
echo $countFunc(); // 3
echo $countFunc(); // 4

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this article is helpful to everyone's PHP programming.


Related articles: