A WordPress-centric search engine for devs and theme authors



wp_array_get ›

Since5.6.0
Deprecatedn/a
wp_array_get ( $array, $path, $default = array() )
Parameters: (3)
  • (array) $array An array from which we want to retrieve some information.
    Required: Yes
  • (array) $path An array of keys describing the path with which to retrieve information.
    Required: Yes
  • (array) $default The return value if the path is not set on the array, or if the types of array and path are not arrays.
    Required: No
    Default: array()
Returns:
  • (array) An array matching the path specified.
Defined at:
Codex:

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

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.


Source

function wp_array_get( $array, $path, $default = array() ) {
	// Confirm input values are expected type to avoid notice warnings.
	if ( ! is_array( $array ) || ! is_array( $path ) ) {
		return $default;
	}

	$path_length = count( $path );

	for ( $i = 0; $i < $path_length; ++$i ) {
		if ( ! isset( $array[ $path[ $i ] ] ) ) {
			return $default;
		}
		$array = $array[ $path[ $i ] ];
	}

	return $array;
}