Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Forked from anonymous/gist:4478798
Last active December 10, 2015 19:08
Show Gist options
  • Save jasonrhodes/4478806 to your computer and use it in GitHub Desktop.
Save jasonrhodes/4478806 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,
"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