Automattic\WooCommerce\Internal\Admin

CategoryLookup::unflatten_terms()protectedWC 1.0

Convert flat terms array into nested array.

Method of the class: CategoryLookup{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->unflatten_terms( $hierarchy, $terms, $parent );
$hierarchy(array) (required) (passed by reference — &)
Array to put terms into.
$terms(array) (required) (passed by reference — &)
Array of terms (id=>parent).
$parent(int)
Parent ID.

CategoryLookup::unflatten_terms() code WC 8.7.0

protected function unflatten_terms( &$hierarchy, &$terms, $parent = 0 ) {
	foreach ( $terms as $term_id => $parent_id ) {
		if ( (int) $parent_id === $parent ) {
			$hierarchy[ $term_id ] = array(
				'term_id'     => $term_id,
				'descendants' => array(),
			);
			unset( $terms[ $term_id ] );
		}
	}
	foreach ( $hierarchy as $term_id => $terms_array ) {
		$this->unflatten_terms( $hierarchy[ $term_id ]['descendants'], $terms, $term_id );
	}
}