Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

_wp_array_get( array $input_array, array $path, mixed $default_value = null ): mixed

Accesses an array in depth based on a path of keys.


Description

It is the PHP equivalent of JavaScript’s lodash.get() and mirroring it may help other components retain some symmetry between client and server implementations.

Example usage:

$input_array = array(
    'a' => array(
        'b' => array(
            'c' => 1,
        ),
    ),
);
_wp_array_get( $input_array, array( 'a', 'b', 'c' ) );

Top ↑

Parameters

$input_array array Required
An array from which we want to retrieve some information.
$path array Required
An array of keys describing the path with which to retrieve information.
$default_value mixed Optional
The return value if the path does not exist within the array, or if $input_array or $path are not arrays.

Default: null


Top ↑

Return

mixed The value from the path specified.


Top ↑

Source

File: wp-includes/functions.php. View all references

function _wp_array_get( $input_array, $path, $default_value = null ) {
	// Confirm $path is valid.
	if ( ! is_array( $path ) || 0 === count( $path ) ) {
		return $default_value;
	}

	foreach ( $path as $path_element ) {
		if (
			! is_array( $input_array ) ||
			( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) ||
			! array_key_exists( $path_element, $input_array )
		) {
			return $default_value;
		}
		$input_array = $input_array[ $path_element ];
	}

	return $input_array;
}


Top ↑

Changelog

Changelog
Version Description
5.6.0 Introduced.

Top ↑

User Contributed Notes

You must log in before being able to contribute a note or feedback.