wp_is_numeric_array()WP 4.4.0

Checks if the specified variable is an numeric-indexed array.

The function checks an array with any numeric indexes, not just an index array. That is, this function is not suitable if you want only an index array to be checked, e.g: [ 'one', 'two' ] but not [ 1=>'one', 2=>'two' ].

1 time — 0.000016 sec (very fast) | 50000 times — 0.03 sec (speed of light) | PHP 7.0.4, WP 4.4.2

No Hooks.

Returns

true|false. true if passed an array variable and all indexes of that array are numbers. Otherwise it returns false.

Usage

wp_is_numeric_array( $data );
$data(mixed) (required)
Variable to check.

Examples

0

#1 Check if the variable is an array with numeric indexes.

Demonstration of the function:

wp_is_numeric_array('foo'); // false

wp_is_numeric_array(['foo'=>'bar', 'foo2'=>'bar2']); // false

wp_is_numeric_array(['foo', 'bar']); // true

wp_is_numeric_array([ 5=>'foo', 10=>'bar' ]); // true
0

#2 Check if the array is an index (sequential) array

A numeric index array and an index array are different arrays. An index array is an array with numeric indexes that are 0, 1, 2 ... 999.

function is_indexed_array( array $data ){
	return array_keys( $data ) === range( 0, count( $data ) - 1 );
}

is_indexed_array( ['foo', 'bar'] ); // true
is_indexed_array( [ 5=>'foo', 10=>'bar' ] ); // false

Changelog

Since 4.4.0 Introduced.

Code of wp_is_numeric_array() WP 5.9.3

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;
}