函数文档

rest_are_values_equal()

💡 云策文档标注

概述

rest_are_values_equal() 函数用于检查两个值是否相等,遵循 JSON Schema 语义。它处理数组、整数和浮点数等类型,并忽略对象属性顺序。

关键要点

  • 函数检查两个值的相等性,基于 JSON Schema 语义,要求值已预先清理或强制转换为原生类型。
  • 对于数组,递归比较元素;对于整数和浮点数,进行类型转换后比较;其他情况使用严格相等运算符。
  • 参数为 $value1 和 $value2,返回布尔值 true 或 false。

代码示例

function rest_are_values_equal( $value1, $value2 ) {
	if ( is_array( $value1 ) && is_array( $value2 ) ) {
		if ( count( $value1 ) !== count( $value2 ) ) {
			return false;
		}

		foreach ( $value1 as $index => $value ) {
			if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) {
				return false;
			}
		}

		return true;
	}

	if ( is_int( $value1 ) && is_float( $value2 )
		|| is_float( $value1 ) && is_int( $value2 )
	) {
		return (float) $value1 === (float) $value2;
	}

	return $value1 === $value2;
}

📄 原文内容

Checks the equality of two values, following JSON Schema semantics.

Description

Property order is ignored for objects.

Values must have been previously sanitized/coerced to their native types.

Parameters

$value1mixedrequired
The first value to check.
$value2mixedrequired
The second value to check.

Return

bool True if the values are equal or false otherwise.

Source

function rest_are_values_equal( $value1, $value2 ) {
	if ( is_array( $value1 ) && is_array( $value2 ) ) {
		if ( count( $value1 ) !== count( $value2 ) ) {
			return false;
		}

		foreach ( $value1 as $index => $value ) {
			if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) {
				return false;
			}
		}

		return true;
	}

	if ( is_int( $value1 ) && is_float( $value2 )
		|| is_float( $value1 ) && is_int( $value2 )
	) {
		return (float) $value1 === (float) $value2;
	}

	return $value1 === $value2;
}

Changelog

Version Description
5.7.0 Introduced.