Quick actions

cmd+k|ctrl+k

Navigation

Languages

Functional Thinking Ex2

Snippet info

Language

Php

Visibility

public

Author

amacgregor

Created

2016-04-24T19:06:46Z

Updated

2016-04-25T11:33:57Z

<?php
/**
 * Example for article: http://coderoncode.com/functional-programming-the-paradigm-for-the-next-generation.html
 */
 
 $sumList = [5,'four','two',10,'one',28,6,'five'];
 $sum     = 0;
 $numDict = [ 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'];

 
 foreach($sumList as $value)
 {
    $arr = array_flip($numDict); 
    if(is_string($value)) {
        $value = $arr[$value];
    }
    $sum += $value;
 }
 
 echo $sum;
INFO