/** * Filters a $tree on depth and parent. This allows you to * get, for example, all of the terms at a certain depth, * or all of the terms of a certain parent at a certain depth. * * @param $tree * As in the result of taxonomy_get_tree() * @param $depth * The integer value of the depth. $tree will be filtered to * only contain terms with this depth ($term->depth). * @param $parent * A tid. $tree will be filtered to only contain the children of * this tid. */ function taxonomy_filter_tree(&$tree, $depth = NULL, $parent = NULL) { if ($count = count($tree)) { if (!is_null($depth)) { $tree = array_values(array_filter(array_map('taxonomy_filter_on_depth', $tree, array_fill(0, $count, $depth)), 'taxonomy_filter_not_null')); } } if ($count = count($tree)) { if (!is_null($parent)) { $tree = array_values(array_filter(array_map('taxonomy_filter_on_parent', $tree, array_fill(0, $count, $parent)), 'taxonomy_filter_not_null')); } } } /** * Utility function used by taxonomy_filter_tree */ function taxonomy_filter_on_depth($term, $depth) { return ($term->depth == $depth) ? $term : NULL; } /** * Utility function used by taxonomy_filter_tree */ function taxonomy_filter_on_parent($term, $parent) { return in_array($parent, $term->parents) ? $term : NULL; } /** * Utility function used by taxonomy_filter_tree */ function taxonomy_filter_not_null($term) { return !is_null($term); }