Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Last active January 6, 2017 02:57
Show Gist options
  • Save aloha1003/1c9bafb67c4feb88aea0bc94ff975796 to your computer and use it in GitHub Desktop.
Save aloha1003/1c9bafb67c4feb88aea0bc94ff975796 to your computer and use it in GitHub Desktop.
<?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