Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Created March 15, 2023 12:59
Show Gist options
  • Save kmuenkel/200315052f307efe29a50df1fe383aab to your computer and use it in GitHub Desktop.
Save kmuenkel/200315052f307efe29a50df1fe383aab to your computer and use it in GitHub Desktop.
Wildcard support for get path
<?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