Last active
January 6, 2017 02:57
-
-
Save aloha1003/1c9bafb67c4feb88aea0bc94ff975796 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
//Enter your code here, enjoy! | |
function arrayListPointer(...$index): Closure{ | |
$item = array_shift($index); | |
$composeIndex = count($index) > 0 ? arrayListPointer(...$index) : function($x) { | |
return $x; | |
}; | |
return function($x) use ($item, $composeIndex) { | |
return '['.$item.']'.$composeIndex($x); | |
}; | |
} | |
function getValue($key, $array, $defaultValue = null) | |
{ | |
if (preg_match_all('#\[(?P<keys>.*?)\]#', $key, $matches)) { | |
$keys = $matches['keys']; | |
$current = $array; | |
foreach ($keys as $k) { | |
if (!isset($current[$k])) { | |
return $defaultValue; | |
} | |
$current = $current[$k]; | |
} | |
return $current; | |
} | |
return $defaultValue; | |
} | |
$t = arrayListPointer('aa', 'bb', 'cc', 'dd', 'ee'); | |
echo '<PRE>'; | |
print_r(getValue($t(''), $ary)); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment