PHP code example of park-brian / functional-array

1. Go to this page and download the library: Download park-brian/functional-array library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

park-brian / functional-array example snippets




use FunctionalArray\FunctionalArray as FnArray;

$sum = FnArray::create(range(1, 10))
  ->map     (function($value) { return $value * 2;  })
  ->filter  (function($value) { return $value > 10; })
  ->reduce  (function($acc = 0, $value) { return $acc + $value; });

echo $sum;

$fruitRatings = [
    'apples' => 70,
    'bananas' => 80,
    'cherries' => 90,
    'dragonfruit' => 100
];

$favoriteFruits = FnArray::create($fruitRatings)
  ->filter (function($value)        { return $value >= 80; })
  ->map    (function($value, $key)  { return "Rating for $key: $value"; })
  ->reduce (function($acc, $value)  { return " $acc \n $value"; })

echo $favoriteFruits;