-
-
Save jasonrhodes/4478806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sum($a, $b) | |
{ | |
return $a + $b; | |
} | |
function myfunc($input) | |
{ | |
if(is_array($input)) | |
{ | |
$counts = array_map("myfunc", $input); | |
return array_reduce($counts, "sum"); | |
} | |
else if(is_int($input)) | |
{ | |
return $input; | |
} | |
else | |
{ | |
return array_reduce(array_values($input), "sum"); | |
} | |
} | |
$conversionSets = array( | |
"100" => array( | |
array( | |
"conversions" => 22, | |
"apples" => 5 | |
), | |
array( | |
"conversions" => 8 | |
) | |
), | |
"200" => array( | |
array( | |
"conversions" => 4 | |
), | |
array( | |
"conversions" => 16 | |
) | |
) | |
); | |
print(myfunc(45)); # => 45 | |
print("\n"); | |
print(myfunc(array(1, 2, 3))); # => 6 | |
print("\n"); | |
print(myfunc(array("hi" => 1, "hai" => 2))); # => 3 | |
print("\n"); | |
print(myfunc($conversionSets)); # => BZZT, 55 instead of 50 | |
print("\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment