WP_Theme_JSON::remove_keys_not_in_schema( array $tree, array $schema ): array

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

Description

It is recursive and modifies the input in-place.

Parameters

$treearrayrequired
Input to process.
$schemaarrayrequired
Schema to adhere to.

Return

array The modified $tree.

Source

 *
 * It is recursive and modifies the input in-place.
 *
 * @since 5.8.0
 *
 * @param array $tree   Input to process.
 * @param array $schema Schema to adhere to.
 * @return array The modified $tree.
 */
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 ] ) ) {

Changelog

VersionDescription
5.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.