Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4478798 to your computer and use it in GitHub Desktop.
Save anonymous/4478798 to your computer and use it in GitHub Desktop.
<?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
),
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)); # => 50
print("\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment