Skip to content

Instantly share code, notes, and snippets.

@iampoul
Last active December 19, 2016 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iampoul/7e64e1d5334dc5a3333e39da412664b7 to your computer and use it in GitHub Desktop.
Save iampoul/7e64e1d5334dc5a3333e39da412664b7 to your computer and use it in GitHub Desktop.
/**
* @param array $array
* @param string $child
* @return array
*/
public function find_child_array_values_by_key(array $array, string $child): array
{
foreach (
new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::CATCH_GET_CHILD)
as $key => $value)
{
if($key !== $child && is_array($value))
{
$this->find_child_array_values_by_key($value, $child);
}
elseif($key === $child && is_array($value))
{
return $value;
}
else
{
return [];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment