函数文档

wp_is_numeric_array()

💡 云策文档标注

概述

wp_is_numeric_array() 函数用于检查变量是否为数字索引数组。它通过判断数组键是否全为数字来确定是否为列表。

关键要点

  • 参数 $data 为必需,类型为 mixed,表示要检查的变量。
  • 返回值为 bool 类型,指示变量是否为列表(即数字索引数组)。
  • 函数内部先检查是否为数组,然后过滤字符串键,若没有字符串键则返回 true。

代码示例

function wp_is_numeric_array( $data ) {
	if ( ! is_array( $data ) ) {
		return false;
	}

	$keys        = array_keys( $data );
	$string_keys = array_filter( $keys, 'is_string' );

	return count( $string_keys ) === 0;
}

注意事项

  • 该函数自 WordPress 4.4.0 版本引入。
  • 相关函数包括 rest_is_array() 等,用于处理数组或类似数组的数据。

📄 原文内容

Determines if the variable is a numeric-indexed array.

Parameters

$datamixedrequired
Variable to check.

Return

bool Whether the variable is a list.

Source

function wp_is_numeric_array( $data ) {
	if ( ! is_array( $data ) ) {
		return false;
	}

	$keys        = array_keys( $data );
	$string_keys = array_filter( $keys, 'is_string' );

	return count( $string_keys ) === 0;
}

Changelog

Version Description
4.4.0 Introduced.