Created
March 15, 2023 12:59
-
-
Save kmuenkel/200315052f307efe29a50df1fe383aab to your computer and use it in GitHub Desktop.
Wildcard support for get path
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 | |
if (!function_exists('get_path')) { | |
function get_path(array $data, $path) | |
{ | |
$nodes = is_string($path) ? explode('.', $path) : $path; | |
$node = array_shift($nodes); | |
$getNext = function ($item) use ($nodes) { | |
return $nodes ? (is_array($item) ? get_path($item, $nodes) : null) : $item; | |
}; | |
return strpos($node, '*') !== false ? array_map($getNext, array_filter($data, function ($key) use ($node) { | |
return fnmatch($node, $key); | |
}, ARRAY_FILTER_USE_KEY)) : $getNext(Arr::get($data, $node)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment