WP_Theme_JSON::remove_keys_not_in_schema()protected staticWP 5.8.0

Given a tree, removes the keys that are not present in the schema.

It is recursive and modifies the input in-place.

Method of the class: WP_Theme_JSON{}

No Hooks.

Return

Array. The modified $tree.

Usage

$result = WP_Theme_JSON::remove_keys_not_in_schema( $tree, $schema );
$tree(array) (required)
Input to process.
$schema(array) (required)
Schema to adhere to.

Changelog

Since 5.8.0 Introduced.

WP_Theme_JSON::remove_keys_not_in_schema() code WP 6.1.1

protected static function remove_keys_not_in_schema( $tree, $schema ) {
	$tree = array_intersect_key( $tree, $schema );

	foreach ( $schema as $key => $data ) {
		if ( ! isset( $tree[ $key ] ) ) {
			continue;
		}

		if ( is_array( $schema[ $key ] ) && is_array( $tree[ $key ] ) ) {
			$tree[ $key ] = static::remove_keys_not_in_schema( $tree[ $key ], $schema[ $key ] );

			if ( empty( $tree[ $key ] ) ) {
				unset( $tree[ $key ] );
			}
		} elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $key ] ) ) {
			unset( $tree[ $key ] );
		}
	}

	return $tree;
}