Tags: PHP
I wrote this algorithm about a year ago, but just fixed it up to be re-usable. The class recursively traverses any iterable object and returns an array of found properties by key name.
There is an optional "$first" parameter, which forces only the first result to be returned.
class PropertyFinder
{
public function get($haystack = [], $needle = '', $first = false)
{
return array_filter($this->traverse($haystack, $needle, $first), function ($value) {
return !empty($value);
});
}
protected function traverse($haystack, $needle, $first, $result = [])
{
if (! is_iterable($haystack)) {
return null;
}
foreach ($haystack as $key => $val) {
if ($key === $needle) {
if ($first) {
return [$val];
} else {
$result[] = $val;
}
} else {
$foundProperty = $this->traverse($val, $needle, $first, $result);
if (!is_null($foundProperty)) {
$result = $foundProperty;
}
}
}
return $result;
}
}
©2023, kirillsimin.com