_wp_array_get

The timeline below displays how wordpress function _wp_array_get has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * 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.
 *
 * Example usage:
 *
 *     $input_array = array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *     _wp_array_get( $input_array, array( 'a', 'b', 'c' ) );
 *
 * @internal
 *
 * @since 5.6.0
 * @access private
 *
 * @param array $input_array   An array from which we want to retrieve some information.
 * @param array $path          An array of keys describing the path with which to retrieve information.
 * @param mixed $default_value Optional. The return value if the path does not exist within the array,
 *                             or if `$input_array` or `$path` are not arrays. Default null.
 * @return mixed The value from the path specified.
 */
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($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 (array_key_exists($path_element, $input_array)) {
                $input_array = $input_array[$path_element];
                continue;
            }
        }
        return $default_value;
    }
    return $input_array;
}

WordPress Version: 6.2

/**
 * 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.
 *
 * Example usage:
 *
 *     $input_array = array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *     _wp_array_get( $input_array, array( 'a', 'b', 'c' ) );
 *
 * @internal
 *
 * @since 5.6.0
 * @access private
 *
 * @param array $input_array   An array from which we want to retrieve some information.
 * @param array $path          An array of keys describing the path with which to retrieve information.
 * @param mixed $default_value Optional. The return value if the path does not exist within the array,
 *                             or if `$input_array` or `$path` are not arrays. Default null.
 * @return mixed The value from the path specified.
 */
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;
}

WordPress Version: 6.1

/**
 * 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.
 *
 * Example usage:
 *
 *     $array = array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *     _wp_array_get( $array, array( 'a', 'b', 'c' ) );
 *
 * @internal
 *
 * @since 5.6.0
 * @access private
 *
 * @param array $array   An array from which we want to retrieve some information.
 * @param array $path    An array of keys describing the path with which to retrieve information.
 * @param mixed $default Optional. The return value if the path does not exist within the array,
 *                       or if `$array` or `$path` are not arrays. Default null.
 * @return mixed The value from the path specified.
 */
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;
}

WordPress Version: 5.8

/**
 * 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.
 *
 * Example usage:
 *
 *     $array = array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *     _wp_array_get( $array, array( 'a', 'b', 'c' ) );
 *
 * @internal
 *
 * @since 5.6.0
 * @access private
 *
 * @param array $array   An array from which we want to retrieve some information.
 * @param array $path    An array of keys describing the path with which to retrieve information.
 * @param mixed $default The return value if the path does not exist within the array,
 *                       or if `$array` or `$path` are not arrays.
 * @return mixed The value from the path specified.
 */
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;
}

WordPress Version: 5.6

/**
 * 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.
 *
 * Example usage:
 *
 *     $array = array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *     _wp_array_get( $array, array( 'a', 'b', 'c' );
 *
 * @internal
 *
 * @since 5.6.0
 * @access private
 *
 * @param array $array   An array from which we want to retrieve some information.
 * @param array $path    An array of keys describing the path with which to retrieve information.
 * @param mixed $default The return value if the path does not exist within the array,
 *                       or if `$array` or `$path` are not arrays.
 * @return mixed The value from the path specified.
 */
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;
}