Automattic\WooCommerce\Internal\Utilities

ArrayUtil::filter_null_values_recursive │ public static │ WC 1.0

Recursively filters null values from an array.

This method removes all null values from the array, including nested arrays. Array keys are preserved for associative arrays. For lists (sequential numeric keys starting from 0), the array is reindexed to maintain the list structure.

Метод класса: ArrayUtil{}

Хуков нет.

Возвращает

array. The filtered array with null values removed.

Использование

$result = ArrayUtil::filter_null_values_recursive( $arr ): array;
$arr(массив) (обязательный)
The array to filter.

Код ArrayUtil::filter_null_values_recursive() WC 11.2.1

public static function filter_null_values_recursive( array $arr ): array {
	$is_list  = self::array_is_list( $arr );
	$filtered = array();

	foreach ( $arr as $key => $value ) {
		// Skip null values.
		if ( is_null( $value ) ) {
			continue;
		}

		// Recursively filter nested arrays.
		if ( is_array( $value ) ) {
			$filtered[ $key ] = self::filter_null_values_recursive( $value );
		} else {
			$filtered[ $key ] = $value;
		}
	}

	// Reindex if the original array was a list.
	return $is_list ? array_values( $filtered ) : $filtered;
}