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() WordPress Function

The wp_array_get function is used to retrieve an array element from a given array. The function takes two arguments: the array to retrieve the element from and the key of the element to retrieve. The function returns the value of the element with the given key or false if the element does not exist.

_wp_array_get( array $array, array $path, mixed $default = null ) #

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:

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

Top ↑

Parameters

$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

(mixed)(Optional)The return value if the path does not exist within the array, or if $array or $path are not arrays.

Default value: null


Top ↑

Return

(mixed) The value from the path specified.


Top ↑

Source

File: wp-includes/functions.php

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

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

	return $array;
}


Top ↑

Changelog

Changelog
VersionDescription
5.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More