Title: WP_REST_View_Config_Controller::cast_empty_objects
Published: August 20, 2026

---

# WP_REST_View_Config_Controller::cast_empty_objects( mixed $value, array $schema ): mixed

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#wp--skip-link--target)

Recursively casts empty arrays to objects where the schema types them as objects.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#description)󠁿

PHP cannot distinguish an empty associative array from an empty list, so `json_encode()`
always serializes `array()` as a JSON array (`[]`). The REST schema, however, types
several values as objects, which must encode as `{}`. This walks the value against
its schema and casts any empty, object-typed array to an object. Non-empty associative
arrays already encode as objects, so they are left as arrays and only recursed into
to fix any nested empty objects.

Union schemas (`oneOf`/`anyOf`) are handled only for the empty-array case: an empty
value is cast to an object when any branch allows an object. Such values are not
recursed into, which is sufficient for the form schema where they never contain 
empty nested objects.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#parameters)󠁿

 `$value`mixedrequired

The value to normalize.

`$schema`arrayrequired

The schema node describing the value.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#return)󠁿

 mixed The normalized value, with empty object-typed arrays cast to objects.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#source)󠁿

    ```php
    protected function cast_empty_objects( $value, $schema ) {
    	if ( ! is_array( $value ) || ! is_array( $schema ) ) {
    		return $value;
    	}

    	if ( isset( $schema['oneOf'] ) || isset( $schema['anyOf'] ) ) {
    		$branches = $schema['oneOf'] ?? $schema['anyOf'];
    		if ( array() === $value ) {
    			foreach ( $branches as $branch ) {
    				if ( is_array( $branch ) && in_array( 'object', (array) ( $branch['type'] ?? array() ), true ) ) {
    					return (object) array();
    				}
    			}
    		}
    		return $value;
    	}

    	$types = (array) ( $schema['type'] ?? array() );

    	if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {
    		foreach ( $value as $index => $item ) {
    			$value[ $index ] = $this->cast_empty_objects( $item, $schema['items'] );
    		}
    		return $value;
    	}

    	if ( in_array( 'object', $types, true ) ) {
    		if ( isset( $schema['properties'] ) ) {
    			foreach ( $schema['properties'] as $property => $property_schema ) {
    				if ( array_key_exists( $property, $value ) ) {
    					$value[ $property ] = $this->cast_empty_objects( $value[ $property ], $property_schema );
    				}
    			}
    		}
    		if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
    			foreach ( $value as $key => $item ) {
    				if ( isset( $schema['properties'][ $key ] ) ) {
    					continue;
    				}
    				$value[ $key ] = $this->cast_empty_objects( $item, $schema['additionalProperties'] );
    			}
    		}

    		// Empty object-typed arrays must serialize as {} to match the schema.
    		if ( array() === $value ) {
    			return (object) array();
    		}
    	}

    	return $value;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php#L190)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php#L190-L240)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_View_Config_Controller::cast_empty_objects()](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/)`wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php` |

Recursively casts empty arrays to objects where the schema types them as objects.

  |

| Used by | Description | 
| [WP_REST_View_Config_Controller::cast_empty_objects()](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/)`wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php` |

Recursively casts empty arrays to objects where the schema types them as objects.

  | 
| [WP_REST_View_Config_Controller::get_items()](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/get_items/)`wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php` |

Returns the default view configuration for the given entity type.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_view_config_controller/cast_empty_objects/?output_format=md#changelog)󠁿

| Version | Description | 
| [7.1.0](https://developer.wordpress.org/reference/since/7.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_view_config_controller%2Fcast_empty_objects%2F)
before being able to contribute a note or feedback.