wpseek.com
A WordPress-centric search engine for devs and theme authors
_wp_array_get is private and should not be used in themes or plugins directly.
_wp_array_get › WordPress Function
Since5.6.0
Deprecatedn/a
› _wp_array_get ( $input_array, $path, $default_value = null )
| Access: |
|
| Parameters: (3) |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Accesses an array in depth based on a path of keys.
It is the PHP equivalent of JavaScript'slodash.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' ) );Source
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 ) ) {
return $default_value;
}
if ( is_string( $path_element )
|| is_integer( $path_element )
|| null === $path_element
) {
/*
* Check if the path element exists in the input array.
* We check with `isset()` first, as it is a lot faster
* than `array_key_exists()`.
*/
if ( isset( $path_element, $input_array[ $path_element ] ) ) {
$input_array = $input_array[ $path_element ];
continue;
}
/*
* If `isset()` returns false, we check with `array_key_exists()`,
* which also checks for `null` values.
*/
if ( isset( $path_element ) && array_key_exists( $path_element, $input_array ) ) {
$input_array = $input_array[ $path_element ];
continue;
}
}
return $default_value;
}
return $input_array;
}