So here is a function that can be used just like WordPress get_categories but returns a hierarchical array. For this there is a child_categories
property added to each WP_TERM
object:
function get_categories_hierarchical( $args = array() ) {
if( !isset( $args[ 'parent' ] ) ) $args[ 'parent' ] = 0;
$categories = get_categories( $args );
foreach( $categories as $key => $category ):
$args[ 'parent' ] = $category->term_id;
$categories[ $key ]->child_categories = get_categories_hierarchical( $args );
endforeach;
return $categories;
}
This function can be used for example like this (to build a row of HTML select inputs out of the first two levels of the category hierarchy):
<?php if( $categories = get_categories_hierarchical() ): foreach( $categories as $category ): ?>
<label for="filter_cat_<?php echo $category->term_id; ?>"><?php echo $category->name; ?></label>
<select name="filter_cat_<?php echo $category->term_id; ?>" id="filter_cat_<?php echo $category->term_id; ?>">
<?php if( $category->child_categories ) foreach( $category->child_categories as $subcategory ): ?>
<option value="<?php echo $subcategory->term_id; ?>"<?php if( $_GET[ 'filter_cat_' . $category->term_id ] == $subcategory->term_id ) echo ' selected'; ?>><?php echo $subcategory->name; ?></option>
<?php endforeach; ?>
</select>
<?php endforeach; endif; ?>
Also check out wp_dropdown_categories and wp_list_categories wich already may do what you need without the need of an additional function.